class Hash
This was copied from: github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/hash/deep_transform_values.rb
def _deep_transform_values_in_object(object, &block)
def _deep_transform_values_in_object(object, &block) case object when Hash object.transform_values { |value| _deep_transform_values_in_object(value, &block) } when Array object.map { |e| _deep_transform_values_in_object(e, &block) } else yield(object) end end
def _deep_transform_values_in_object!(object, &block)
def _deep_transform_values_in_object!(object, &block) case object when Hash object.transform_values! { |value| _deep_transform_values_in_object!(value, &block) } when Array object.map! { |e| _deep_transform_values_in_object!(e, &block) } else yield(object) end end
def deep_transform_values(&block)
hash.deep_transform_values{ |value| value.to_s.upcase }
hash = { person: { name: 'Rob', age: '28' } }
nested hashes and arrays.
This includes the values from the root hash and from all
Returns a new hash with all values converted by the block operation.
def deep_transform_values(&block) _deep_transform_values_in_object(self, &block) end
def deep_transform_values!(&block)
This includes the values from the root hash and from all
Destructively converts all values by using the block operation.
def deep_transform_values!(&block) _deep_transform_values_in_object!(self, &block) end