Iterating over a hash map

Hello,
I’m trying to iterate over a hash map I created, but its not working
very well… I’ve been experimenting in IRB like so:

compare_hash = {}
compare_hash[“item1”] = [“subitem1”,“subitem2”]
compare_hash[“item2”] = [“subitem1”,“subitem2”]

compare_hash.each do |item|
puts item[0]
end

instead of giving me the expected results of:
subitem1
subitem2

it gives me only the names:
item1
item2

Does anybody know how to do this properly?

Thanks!

  • Jeff M.

On 25 Apr 2008, at 22:17, Jeff M. wrote:

puts item[0]
end

compare_hash.each do |key, value|
puts key #will output item1, item2
puts value #will out put the values
end

Fred

thanks! that worked!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs