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!
Written by Qi Liu
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ruby
Authors
Related Tags
#ruby
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#