合并红宝石一些复杂的哈希(merge some complex hashes in ruby)

2019-09-17 09:08发布

我想下面的哈希值合并到一起。

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

特别是,我期望的结果是:

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

我已经想尽办法为:

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

不幸的是,似乎迎刃而解了我。 任何意见将非常感激。

Answer 1:

这应该为你工作:

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


文章来源: merge some complex hashes in ruby