Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/icuke/core_ext.rb

Instance Method Summary collapse

Instance Method Details

#deep_merge(hash) ⇒ Object

Merges self with another hash, recursively.

This code was lovingly stolen from some random gem: gemjack.com/gems/tartan-0.1.1/classes/Hash.html

Thanks to whoever made it.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/icuke/core_ext.rb', line 38

def deep_merge(hash)
  target = dup
  
  hash.keys.each do |key|
    if hash[key].is_a? Hash and self[key].is_a? Hash
      target[key] = target[key].deep_merge(hash[key])
      next
    end
    
    target[key] = hash[key]
  end
  
  target
end