Sha256: 0e51f8d35b23809fe9927e80752140a959c947026b0f8978deb6b4ea7054fb21

Contents?: true

Size: 1.42 KB

Versions: 66

Compression:

Stored size: 1.42 KB

Contents

# frozen_string_literal: true

class Hash
  # 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"}}
  def deep_transform_values(&block)
    _deep_transform_values_in_object(self, &block)
  end

  # Destructively converts all values by using the block operation.
  # This includes the values from the root hash and from all
  # nested hashes and arrays.
  def deep_transform_values!(&block)
    _deep_transform_values_in_object!(self, &block)
  end

  private
    # Support methods for deep transforming nested hashes and arrays.
    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)
      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
end

Version data entries

66 entries across 64 versions & 11 rubygems

Version Path
activesupport-8.0.2 lib/active_support/core_ext/hash/deep_transform_values.rb
tailscale_middleware-0.0.3 vendor/cache/ruby/3.4.0/gems/activesupport-8.0.1/lib/active_support/core_ext/hash/deep_transform_values.rb
trusty-cms-7.0.9.1 vendor/bundle/ruby/3.3.0/gems/activesupport-7.0.8.7/lib/active_support/core_ext/hash/deep_transform_values.rb
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/activesupport-7.1.3.4/lib/active_support/core_ext/hash/deep_transform_values.rb
activesupport-8.0.1 lib/active_support/core_ext/hash/deep_transform_values.rb
activesupport-8.0.0.1 lib/active_support/core_ext/hash/deep_transform_values.rb
activesupport-7.2.2.1 lib/active_support/core_ext/hash/deep_transform_values.rb
activesupport-7.1.5.1 lib/active_support/core_ext/hash/deep_transform_values.rb
activesupport-7.0.8.7 lib/active_support/core_ext/hash/deep_transform_values.rb
activesupport-8.0.0 lib/active_support/core_ext/hash/deep_transform_values.rb
activesupport-7.2.2 lib/active_support/core_ext/hash/deep_transform_values.rb
activesupport-7.1.5 lib/active_support/core_ext/hash/deep_transform_values.rb
activesupport-8.0.0.rc2 lib/active_support/core_ext/hash/deep_transform_values.rb
activesupport-7.2.1.2 lib/active_support/core_ext/hash/deep_transform_values.rb
activesupport-7.1.4.2 lib/active_support/core_ext/hash/deep_transform_values.rb
activesupport-7.0.8.6 lib/active_support/core_ext/hash/deep_transform_values.rb
activesupport-8.0.0.rc1 lib/active_support/core_ext/hash/deep_transform_values.rb
activesupport-7.2.1.1 lib/active_support/core_ext/hash/deep_transform_values.rb
activesupport-7.1.4.1 lib/active_support/core_ext/hash/deep_transform_values.rb
activesupport-7.0.8.5 lib/active_support/core_ext/hash/deep_transform_values.rb