Class: Hash

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

Instance Method Summary collapse

Instance Method Details

#deep_merge(other_hash, &block) ⇒ Object



2
3
4
# File 'lib/core/ext/hash.rb', line 2

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

#deep_merge!(other_hash, &block) ⇒ Object



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

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

#except(*less_keys) ⇒ Object



24
25
26
# File 'lib/core/ext/hash.rb', line 24

def except(*less_keys)
  slice(*keys - less_keys)
end

#slice(*keep_keys) ⇒ Object



18
19
20
21
22
# File 'lib/core/ext/hash.rb', line 18

def slice(*keep_keys)
  h                                              = {}
  keep_keys.each { |key| has_key?(key) && h[key] = fetch(key, nil) }
  h
end