Saggex
Saggex

Reputation: 3500

Puts Hash in Ruby

I have this hash, and want to print it:

{:date=>#<Date: 2013-03-29 ((2456381j,0s,0n),+0s,2299161j)>, :name=>"Karfreitag", :regions=>[:de]}
{:date=>#<Date: 2013-04-01 ((2456384j,0s,0n),+0s,2299161j)>, :name=>"Ostermontag", :regions=>[:de]}
{:date=>#<Date: 2013-05-01 ((2456414j,0s,0n),+0s,2299161j)>, :name=>"Tag der Arbeit", :regions=>[:de]}

I tried it with:

def print_holiday(a)        
  a.each do |date|
    puts date["name"] + " " + date["date"]
  end                   
end

But all I get are some empty lines. Got any ideas?

I'm using the holidays gem.

Upvotes: 2

Views: 234

Answers (1)

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230521

A string is not a symbol. Keys in your hash are symbols, but you're trying to access values through strings.

Try this:

puts date[:name] + " " + date[:date]

The Difference Between Ruby Symbols and Strings

Upvotes: 10

Related Questions