8

I have a hash, I am trying to extract the keys and values for it. The hash has a nested hash and/or array of hashes.

After checking this site and few samples I got the key and values. But having difficulty in extracting if its a array of hashes.

Example:

{
  :key1 => 'value1',
  :key2 => 'value2',
  :key3 => {
    :key4 => [{:key4_1 => 'value4_1', :key4_2 => 'value4_2'}],
    :key5 => 'value5'
  },
  :key6 => {
    :key7 => [1,2,3],
    :key8 => {
      :key9 => 'value9'
    }
  }
}

So far I have below code from how do i loop over a hash of hashes in ruby and Iterate over an deeply nested level of hashes in Ruby

def ihash(h)
  h.each_pair do |k,v|
    if v.is_a?(Hash) || v.is_a?(Array)
      puts "key: #{k} recursing..."
      ihash(v)
    else
      # MODIFY HERE! Look for what you want to find in the hash here
      puts "key: #{k} value: #{v}"
    end
  end
end

But it fails at v.is_hash?(Array) or v.is_a?(Array).

Am I missing something?

Community
  • 1
  • 1
user2300908
  • 165
  • 1
  • 1
  • 12

3 Answers3

20

It is not entirely clear what you might want, but both Array and Hash implement each (which, in the case of Hash, is an alias for each_pair).

So to get exactly the output you would get if your method would work, you could implement it like this:

def iterate(h)
  h.each do |k,v|
    # If v is nil, an array is being iterated and the value is k. 
    # If v is not nil, a hash is being iterated and the value is v.
    # 
    value = v || k

    if value.is_a?(Hash) || value.is_a?(Array)
      puts "evaluating: #{value} recursively..."
      iterate(value)
    else
      # MODIFY HERE! Look for what you want to find in the hash here
      # if v is nil, just display the array value
      puts v ? "key: #{k} value: #{v}" : "array value #{k}"
    end
  end
end
Beat Richartz
  • 9,474
  • 1
  • 33
  • 50
1

You are calling ihash(v), if v is an Array or a Hash. each_pair method call will fail for arrays.

0

You could skip the recursion and use something more like this:

a = {}
a[0] = { first: "first" }
a[1] = [{ second: "second_1" }, { second: "second_2" }]

a.each_pair do |k1, v1|
  puts "======== key: #{k1}, value: ========"

  if v1.class == Hash
    v1.each_pair do |k2, v2|
      puts "key_2: #{k2}, value: #{v2}"
    end
  else
    v1.each do |h|
      h.each_pair do |k2, v2|
        puts "hash #{v1.index(h)} => key_2: #{k2}, value: #{v2}"
      end
    end
  end
end

Output:

======== key: 0, value: ========
key_2: first, value: first
======== key: 1, value: ========
hash 0 => key_2: second, value: second_1
hash 1 => key_2: second, value: second_2
Gray Kemmey
  • 976
  • 8
  • 12