Class: Hash

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

Instance Method Summary collapse

Instance Method Details

#deep_merge(second) ⇒ Object



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

def deep_merge(second)
  merger = proc do |_, v1, v2|
    if v1.is_a?(Hash) && v2.is_a?(Hash)
      v1.merge(v2, &merger)
    elsif v1.is_a?(Array) && v2.is_a?(Array)
      v1 | v2
    else
      [:undefined, nil, :nil].include?(v2) ? v1 : v2
    end
  end
  merge(second.to_h, &merger)
end