Class: Object

Inherits:
BasicObject
Defined in:
lib/monkey_patches.rb

Overview

Modify ‘Object`

original from (https://gist.github.com/Integralist/9503099)

None of the above solutions work with a multi-level hash They only work on the first level: :level1=>{“level2”=>“baz”} The following two variations solve the problem in the same way transform hash keys to symbols

Examples:

multi_hash = { 'foo' => 'bar', 'level1' => { 'level2' => 'baz' } }
multi_hash = multi_hash.deep_string_keys

Instance Method Summary collapse

Instance Method Details

#deep_string_keysObject



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/monkey_patches.rb', line 32

def deep_string_keys
  if( is_a?( Hash ) )
    return inject({}) do |memo, (k, v)|
      memo.tap { |m| m[k.to_s] = v.deep_string_keys }
    end
  elsif( is_a?( Array ) )
    return map(&:deep_string_keys)
  end

  self
end

#deep_symbolize_keysObject



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/monkey_patches.rb', line 20

def deep_symbolize_keys
  if( is_a?( Hash ) )
    return inject({}) do |memo, (k, v)|
      memo.tap { |m| m[k.to_sym] = v.deep_string_keys }
    end
  elsif( is_a?( Array ) )
    return map(&:deep_string_keys)
  end

  self
end