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) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/core_ext/hash.rb', line 18

def deep_merge(other_hash)
  a = self.dup
  other_hash.each do |k, v|
    if v.is_a? Hash
      if a.has_key? k
        a[k] = a[k].deep_merge(v)
      else
        a[k] = v
      end
    else
      a[k] = v
    end
  end
  a
end

#has_nested_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


2
3
4
5
6
7
8
# File 'lib/core_ext/hash.rb', line 2

def has_nested_key?(key)
  return true if has_key?(key)
  values.select{|v| v.is_a?(Hash)}.each do |value|
    return value.has_nested_key?(key)
  end
  return false
end

#nested_key(key) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/core_ext/hash.rb', line 10

def nested_key(key)
  return fetch(key) if has_key?(key)
  values.select{|v| v.is_a?(Hash)}.each do |value|
    return value.nested_key(key)
  end
  return default(key)
end