how to merge hash of hash with same keys in ruby

68 Views Asked by At

I have a two 2nd level hash , and i have to merge both hashes

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

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

output should be

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

I have tried this code

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

But this works for 1st level hash, not for 2nd level hash, hash inside hash

0

There are 0 best solutions below