{:received => 2}, "201202" => {:received => 4 }} h2 = {"201201" -6ren">
gpt4 book ai didi

ruby - 在 ruby​​ 中合并一些复杂的哈希值

转载 作者:数据小太阳 更新时间:2023-10-29 07:50:40 26 4
gpt4 key购买 nike

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

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

不幸的是,两者似乎都不适合我。任何建议将不胜感激。

最佳答案

这应该适合你:

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

关于ruby - 在 ruby​​ 中合并一些复杂的哈希值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10691318/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com