Class: Babl::Utils::Hash

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

Constant Summary collapse

EMPTY =
{}.freeze

Class Method Summary collapse

Class Method Details

.deep_merge(hashes) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/babl/utils/hash.rb', line 8

def deep_merge(hashes)
    filtered_hashes = hashes.reject(&:empty?)
    return EMPTY if filtered_hashes.empty?
    return filtered_hashes.first if filtered_hashes.size == 1

    filtered_hashes.reduce({}) { |out, hash| deep_merge_inplace(out, hash) }
end

.deep_merge_inplace(target, source) ⇒ Object



16
17
18
19
# File 'lib/babl/utils/hash.rb', line 16

def deep_merge_inplace(target, source)
    source.each { |k, v| target[k] = deep_merge_inplace(target[k] || {}, v) }
    target
end