{:received => 2}, "201202" =>...">

Combine some complex hashes in a ruby

I would like to combine the following hashes together.

 h1 = {"201201" => {:received => 2},   "201202" => {:received => 4 }}
 h2 = {"201201" => {:closed => 1},  "201202" => {:closed => 1 }}

in particular, my expected result:

h1 = {"201201" => {:received => 2, :closed => 1},  "201202" => {:received => 4, :closed => 1 }}

I tried everything like:

h = h1.merge(h2){|key, first, second| {first , second} }

Unfortunately, nothing seemed suitable to me. Any advice would be really appreciated.

+3
source share
1 answer

This should work for you:

h = h1.merge(h2){|key, first, second| first.merge(second)}
+6
source

All Articles