How to get a specific output iterating a hash in Ruby? How to get a specific output iterating a hash in Ruby? ruby ruby

How to get a specific output iterating a hash in Ruby?


hash.each do |key, array|  puts "#{key}-----"  puts arrayend

Regarding order I should add, that in 1.8 the items will be iterated in random order (well, actually in an order defined by Fixnum's hashing function), while in 1.9 it will be iterated in the order of the literal.


The most basic way to iterate over a hash is as follows:

hash.each do |key, value|  puts key  puts valueend


hash.keys.sort.each do |key|  puts "#{key}-----"  hash[key].each { |val| puts val }end