class Hash

Public Instance Methods

deep_transform_values(&block) click to toggle source

Returns a new hash with all values converted by the block operation. This includes the values from the root hash and from all nested hashes and arrays.

hash = { person: { name: 'Rob', age: '28' } }

hash.deep_transform_values{ |value| value.to_s.upcase }
# => {person: {name: "ROB", age: "28"}}
# File lib/futurism/shims/deep_transform_values.rb, line 12
def deep_transform_values(&block)
  _deep_transform_values_in_object(self, &block)
end
deep_transform_values!(&block) click to toggle source

Destructively converts all values by using the block operation. This includes the values from the root hash and from all nested hashes and arrays.

# File lib/futurism/shims/deep_transform_values.rb, line 19
def deep_transform_values!(&block)
  _deep_transform_values_in_object!(self, &block)
end

Private Instance Methods

_deep_transform_values_in_object(object) { |object| ... } click to toggle source

Support methods for deep transforming nested hashes and arrays.

# File lib/futurism/shims/deep_transform_values.rb, line 26
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
_deep_transform_values_in_object!(object) { |object| ... } click to toggle source
# File lib/futurism/shims/deep_transform_values.rb, line 37
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