Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/kielce/helpers.rb

Instance Method Summary collapse

Instance Method Details

#deep_merge(second) ⇒ Object

Merges two hashes recursively. Specificaly, if the value of one item in the hash is itself a hash, merge that nested hash.

Hash#merge by default returns a new Hash containing the key/value pairs of both hashes. If a key appears in both “self” and “second”, the value in “second” takes precedence. Alternately, you can specify a block to be called if a key appears in both Hashes. The “merger” lambda below merges two nested hashes instead of simply choosing the version in “second”



25
26
27
28
# File 'lib/kielce/helpers.rb', line 25

def deep_merge(second)
  merger = proc { |key, v1, v2| Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : v2 }
  self.merge(second, &merger)
end