Class: Object
- Inherits:
- BasicObject
- Defined in:
- lib/monkey_patches.rb
Overview
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 multi_hash = { ‘foo’ => ‘bar’, ‘level1’ => { ‘level2’ => ‘baz’ } } multi_hash = multi_hash.deep_string_keys
Instance Method Summary collapse
Instance Method Details
#deep_string_keys ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/monkey_patches.rb', line 29 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_keys ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/monkey_patches.rb', line 16 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 |