I am trying to merge two Hashes.I got the code from here. As you can see I want to add one more hobby to my list. There are many hobbies. When h1 is formed ,only one hobby was available. When the second hobby arrived I wanted to merge it with the previous hash.
The structure of the Hashes are :
h1 = {"students"=>[{"name"=>"bobby", "hobbies"=>[{"outdoor"=>"cycling"}]}]}
h2 = {"students"=>[{"name"=>"bobby", "hobbies"=>[{"indoor"=>"reading"}]}]}
I ran this code:
res = h1.merge(h2) {|key,val1,val2| val1.merge val2}
The error I got was:
undefined method `merge' for [{"name"=>"bobby", "hobbies"=>[{"outdoor"=>"cycling"}]}]:Array (NoMethodError)
So since array is involved, I don't know what to do. Any Help.
Required Output:
{"students"=>[{"name"=>"bobby", "hobbies"=>[{"outdoor"=>"cycling", "indoor"=>"reading"}]}]}
h1 = {"students"=>[{"name"=>"bobby", "hobbies"=>[{"outdoor"=>"cycling"}]}]}. Also, we don't know what you want to do. What do you want to happen when there are arrays present? Give the output example as well as input examples.