Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/kuzya/core_ext/hash/deep_merge.rb,
lib/kuzya/core_ext/hash/deep_compact.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.deep_compact(hash) ⇒ Object



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

def self.deep_compact(hash)
  result = hash.map do |key, value|
    value = deep_compact(value) if value.is_a?(Hash)

    value = nil if [{}, []].include?(value)
    [key, value]
  end

  result.to_h.compact
end

Instance Method Details

#deep_merge(other_hash, &block) ⇒ Object



17
18
19
# File 'lib/kuzya/core_ext/hash/deep_merge.rb', line 17

def deep_merge(other_hash, &block)
  dup.deep_merge!(other_hash, &block)
end

#deep_merge!(other_hash, &block) ⇒ Object

Copy-pasted from activesupport/lib/active_support/core_ext/hash/deep_merge.rb



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/kuzya/core_ext/hash/deep_merge.rb', line 5

def deep_merge!(other_hash, &block)
  merge!(other_hash) do |key, this_val, other_val|
    if this_val.is_a?(Hash) && other_val.is_a?(Hash)
      this_val.deep_merge(other_val, &block)
    elsif block_given?
      block.call(key, this_val, other_val)
    else
      other_val
    end
  end
end