Method: HashTools#transform_keys_of
- Defined in:
- lib/hash_tools.rb
#transform_keys_of(any, &transformer) ⇒ Hash
Recursively convert hash keys using a block. using a passed block. The block will receive a hash key to be transformed and should return a transformed key For example, to go from uderscored notation to camelized:
h = {'foo_bar' => 1}
transform_keys_of(h) {|k| k.to_s.camelize(:lower) } # => {'fooBar' => 1}
115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/hash_tools.rb', line 115 def transform_keys_of(any, &transformer) case any when Array any.map { |e| transform_keys_of(e, &transformer) } when Hash h = {} any.each_pair do |k, v| h[transformer.call(k.to_s)] = transform_keys_of(v, &transformer) end h else any end end |