如何在ruby中合并具有相同键的哈希哈希

时间:2018-09-17 06:17:31

标签: ruby ruby-on-rails-4 hash ruby-hash

我有两个二级散列,并且我必须合并两个散列

hash1 = {"brands"=>{"likes"=>["gucci", "ck", "armani", "heyo"], "dislikes"=>["lv", "tods", "pols"]}}

hash2 = {"brands"=>{"likes"=>["bampam","plot"], "dislikes"=>["pom", "ui"]}}

输出应为

 hash1 = {"brands"=>{"likes"=>["gucci", "ck", "armani", "heyo","bampam","plot"], "dislikes"=>["lv", "tods", "pols","pom","ui"]}}

我已经尝试过此代码

hash1.merge!(hash2) { |k, o, n| o + n }

但这适用于1级哈希,不适用于2级哈希,哈希内部的哈希

0 个答案:

没有答案