231

I want to get a specific output iterating a Ruby Hash.

This is the Hash I want to iterate over:

hash = {
  1 => ['a', 'b'], 
  2 => ['c'], 
  3 => ['d', 'e', 'f', 'g'], 
  4 => ['h']
}

This is the output I would like to get:

1-----

a

b

2-----

c

3-----

d 

e

f

g

4-----

h

In Ruby, how can I get such an output with my Hash ?

4
  • 3
    If you're iterating a hash and expecting it to be ordered, you probably need to use some other collection type
    – Allen Rice
    Commented Aug 4, 2009 at 13:49
  • can i pass the hash values as radio button option??
    – sts
    Commented Aug 4, 2009 at 13:58
  • am passing the hash as radio button option .. but for the first option am getting radio button, for other values am not getting it.
    – sts
    Commented Aug 4, 2009 at 14:10
  • 1
    @Allen: Hashes are ordered in Ruby 1.9. Rails also provides an OrderedHash (that it uses only sparingly) if you're on Ruby <1.9. See culann.com/2008/01/rails-goodies-activesupportorderedhash Commented Aug 4, 2009 at 15:25

6 Answers 6

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

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.

6
  • 2
    here what if key isn't used anywhere ? . do we need to put a ? in place of key ? ex : |?, array| is this valid syntax ? Commented Mar 16, 2016 at 11:26
  • 1
    @huzefabiyawarwala No, ? is not a valid variable name in Ruby. You can use _, but you don't need to.
    – sepp2k
    Commented Mar 16, 2016 at 13:04
  • 2
    @huzefabiyawarwala Yes, you can write |_, v|
    – sepp2k
    Commented Mar 16, 2016 at 13:42
  • 1
    what use variable name array instead of v or value?
    – jrhicks
    Commented Mar 5, 2017 at 13:53
  • 1
    @jrhicks Because the OP has a hash whose values are arrays. Commented May 13, 2017 at 0:21
93

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

hash.each do |key, value|
  puts key
  puts value
end
2
  • Yea this makes sense. Why does @sepp2k's answer have a #{} on the key? Commented Nov 3, 2018 at 23:16
  • oh never mind. I saw that it was for string interpolation Commented Nov 3, 2018 at 23:17
50
hash.keys.sort.each do |key|
  puts "#{key}-----"
  hash[key].each { |val| puts val }
end
18

Calling sort on a hash converts it into nested arrays and then sorts them by key, so all you need is this:

puts h.sort.map {|k,v| ["#{k}----"] + v}

And if you don't actually need the "----" part, it can be just:

puts h.sort
2
  • The hash keys are numbers, so '[k + "----"]' raises a TypeError (String can't be coerced into Fixnum). You need '[k.to_s + "----"]' Commented Aug 4, 2009 at 19:35
  • True enough. I had letters in my test version. Fixed, using the even better "#{k}----". Commented Aug 5, 2009 at 2:22
14

My one line solution:

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

I think it is pretty easy to read.

1

You can also refine Hash::each so it will support recursive enumeration. Here is my version of Hash::each(Hash::each_pair) with block and enumerator support:

module HashRecursive
    refine Hash do
        def each(recursive=false, &block)
            if recursive
                Enumerator.new do |yielder|
                    self.map do |key, value|
                        value.each(recursive=true).map{ |key_next, value_next| yielder << [[key, key_next].flatten, value_next] } if value.is_a?(Hash)
                        yielder << [[key], value]
                    end
                end.entries.each(&block)
            else
                super(&block)
            end
        end
        alias_method(:each_pair, :each)
    end
end

using HashRecursive

Here are usage examples of Hash::each with and without recursive flag:

hash = {
    :a => {
        :b => {
            :c => 1,
            :d => [2, 3, 4]
        },
        :e => 5
    },
    :f => 6
}

p hash.each, hash.each {}, hash.each.size
# #<Enumerator: {:a=>{:b=>{:c=>1, :d=>[2, 3, 4]}, :e=>5}, :f=>6}:each>
# {:a=>{:b=>{:c=>1, :d=>[2, 3, 4]}, :e=>5}, :f=>6}
# 2

p hash.each(true), hash.each(true) {}, hash.each(true).size
# #<Enumerator: [[[:a, :b, :c], 1], [[:a, :b, :d], [2, 3, 4]], [[:a, :b], {:c=>1, :d=>[2, 3, 4]}], [[:a, :e], 5], [[:a], {:b=>{:c=>1, :d=>[2, 3, 4]}, :e=>5}], [[:f], 6]]:each>
# [[[:a, :b, :c], 1], [[:a, :b, :d], [2, 3, 4]], [[:a, :b], {:c=>1, :d=>[2, 3, 4]}], [[:a, :e], 5], [[:a], {:b=>{:c=>1, :d=>[2, 3, 4]}, :e=>5}], [[:f], 6]]
# 6

hash.each do |key, value|
    puts "#{key} => #{value}"
end
# a => {:b=>{:c=>1, :d=>[2, 3, 4]}, :e=>5}
# f => 6

hash.each(true) do |key, value|
    puts "#{key} => #{value}"
end
# [:a, :b, :c] => 1
# [:a, :b, :d] => [2, 3, 4]
# [:a, :b] => {:c=>1, :d=>[2, 3, 4]}
# [:a, :e] => 5
# [:a] => {:b=>{:c=>1, :d=>[2, 3, 4]}, :e=>5}
# [:f] => 6

hash.each_pair(recursive=true) do |key, value|
    puts "#{key} => #{value}" unless value.is_a?(Hash)
end
# [:a, :b, :c] => 1
# [:a, :b, :d] => [2, 3, 4]
# [:a, :e] => 5
# [:f] => 6

Here is example from the question itself:

hash = {
    1   =>  ["a", "b"], 
    2   =>  ["c"], 
    3   =>  ["a", "d", "f", "g"], 
    4   =>  ["q"]
}

hash.each(recursive=false) do |key, value|
    puts "#{key} => #{value}"
end
# 1 => ["a", "b"]
# 2 => ["c"]
# 3 => ["a", "d", "f", "g"]
# 4 => ["q"]

Also take a look at my recursive version of Hash::merge(Hash::merge!) here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.