简体   繁体   中英

merge some complex hashes in ruby

I'd like to merge the following hashes together.

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

particularly, my expected result is:

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

I have tried every way as:

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

unfortunately, neither seemed to work out fine for me. any advice would be really appreciated.

这应该适合你:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM