8

It does not seem to be documented very much:

hsh.merge(other_hash){|key, oldval, newval| block} → a_hash

http://ruby-doc.org/core/classes/Hash.html#M002880

Wayne Conrad
  • 103,207
  • 26
  • 155
  • 191
dreftymac
  • 31,404
  • 26
  • 119
  • 182

1 Answers1

10

As it might be expected, the resulting hash will contain the value returned by a block for every key which exists in both hashes being merged:

>> h1 = {:a => 3, :b => 5, :c => 6}
=> {:a=>3, :b=>5, :c=>6}
>> h2 = {:a => 4, :b => 7, :d => 8}
=> {:a=>4, :b=>7, :d=>8}
>> h1.merge h2
=> {:a=>4, :b=>7, :c=>6, :d=>8}
>> h1.merge(h2){|k,v1,v2| v1}
=> {:a=>3, :b=>5, :c=>6, :d=>8}
>> h1.merge(h2){|k,v1,v2| v1+v2}
=> {:a=>7, :b=>12, :c=>6, :d=>8}
Mladen Jablanović
  • 43,461
  • 10
  • 90
  • 113