Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/hashr/core_ext/ruby/hash.rb

Instance Method Summary collapse

Instance Method Details

#deep_merge(other) ⇒ Object

deep_merge_hash! by Stefan Rusterholz, see www.ruby-forum.com/topic/142809



20
21
22
23
# File 'lib/hashr/core_ext/ruby/hash.rb', line 20

def deep_merge(other)
  merger = proc { |key, v1, v2| Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : v2 }
  merge(other, &merger)
end

#deep_symbolize_keysObject



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/hashr/core_ext/ruby/hash.rb', line 2

def deep_symbolize_keys
   symbolizer = lambda do |value|
    case value
    when Hash
      value.deep_symbolize_keys
    when Array
      value.map { |value| symbolizer.call(value) }
    else
      value
    end
  end
  inject({}) do |result, (key, value)|
    result[(key.to_sym rescue key) || key] = symbolizer.call(value)
    result
  end
end