Class: Hash

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

Instance Method Summary collapse

Instance Method Details

#deep_merge(hash) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
# File 'lib/hash.rb', line 2

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

#deep_merge!(second) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/hash.rb', line 14

def deep_merge!(second)
  second.each_pair do |k,v|
    if self[k].is_a?(Hash) and second[k].is_a?(Hash)
      self[k].deep_merge!(second[k])
    else
      self[k] = second[k]
    end
  end
end