3

我想将以下哈希合并在一起。

 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} }

不幸的是,对我来说,两者似乎都不是很好。任何建议将不胜感激。

4

1 回答 1

6

这应该适合你:

h = h1.merge(h2){|key, first, second| first.merge(second)}
于 2012-05-21T19:30:39.297 回答