Class: Hash

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

Instance Method Summary collapse

Instance Method Details

#deep_merge(other) ⇒ Object



2
3
4
# File 'lib/foundry/refinements/hash.rb', line 2

def deep_merge(other)
  dup.deep_merge!(other)
end

#deep_merge!(other) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/foundry/refinements/hash.rb', line 6

def deep_merge!(other)
  other.each_pair do |other_key, other_value|
    this_value = self[other_key]
    if this_value.is_a?(Hash) && other_value.is_a?(Hash)
      self[other_key] = this_value.deep_merge(other_value)
    else
      self[other_key] = other_value
    end
  end
  self
end

#without(*keys) ⇒ Object



18
19
20
# File 'lib/foundry/refinements/hash.rb', line 18

def without(*keys)
  dup.without!(*keys)
end

#without!(*keys) ⇒ Object



22
23
24
# File 'lib/foundry/refinements/hash.rb', line 22

def without!(*keys)
  self.reject! { |key, _| keys.include?(key) }; self
end