Sha256: 7e2934901796f42022924b67b2f0824416e63eba43c10aba72cc5894d0bf5ec3

Contents?: true

Size: 932 Bytes

Versions: 29

Compression:

Stored size: 932 Bytes

Contents

class Hash
  # Returns a new hash with the results of running +block+ once for every value.
  # The keys are unchanged.
  #
  #   { a: 1, b: 2, c: 3 }.transform_values { |x| x * 2 } # => { a: 2, b: 4, c: 6 }
  #
  # If you do not provide a +block+, it will return an Enumerator
  # for chaining with other methods:
  #
  #   { a: 1, b: 2 }.transform_values.with_index { |v, i| [v, i].join.to_i } # => { a: 10, b: 21 }
  def transform_values
    return enum_for(:transform_values) { size } unless block_given?
    return {} if empty?
    result = self.class.new
    each do |key, value|
      result[key] = yield(value)
    end
    result
  end

  # Destructively converts all values using the +block+ operations.
  # Same as +transform_values+ but modifies +self+.
  def transform_values!
    return enum_for(:transform_values!) { size } unless block_given?
    each do |key, value|
      self[key] = yield(value)
    end
  end
end

Version data entries

29 entries across 28 versions & 5 rubygems

Version Path
activesupport-5.0.0.beta3 lib/active_support/core_ext/hash/transform_values.rb
activesupport-5.0.0.beta2 lib/active_support/core_ext/hash/transform_values.rb
activesupport-5.0.0.beta1.1 lib/active_support/core_ext/hash/transform_values.rb
core_ext-0.0.5 lib/core_ext/hash/transform_values.rb
core_ext-0.0.4 lib/core_ext/hash/transform_values.rb
activesupport-5.0.0.beta1 lib/active_support/core_ext/hash/transform_values.rb
core_ext-0.0.3 lib/core_ext/hash/transform_values.rb
core_ext-0.0.2 lib/core_ext/hash/transform_values.rb
core_ext-0.0.1 lib/core_ext/hash/transform_values.rb