Last Updated: February 25, 2016
·
380
· lovio

merge_hashes in Ruby

I have several hashes and I want to combine them according to the need.

Firstly, I thought of merge. But it looks complex. e.g.

a.merge(b).merge(c).merge(d).merge(c)

Then, I searched gem activesupport for help and got nothing.

Finally, I wrote one. It reads as follow.

def merge_hashes *hashes
    hashes.inject(:merge)
end

 ap multiple_hash(a:1, b:2, c:3, d: 4)

Ahah! It works!