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
autocompl-0.2.2 test/dummy/vendor/bundle/ruby/2.3.0/gems/activesupport-5.0.1/lib/active_support/core_ext/hash/transform_values.rb
autocompl-0.2.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/activesupport-5.0.1/lib/active_support/core_ext/hash/transform_values.rb
autocompl-0.2.0 test/dummy/vendor/bundle/ruby/2.3.0/gems/activesupport-5.0.1/lib/active_support/core_ext/hash/transform_values.rb
autocompl-0.1.2 test/dummy/vendor/bundle/ruby/2.3.0/gems/activesupport-5.0.1/lib/active_support/core_ext/hash/transform_values.rb
autocompl-0.1.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/activesupport-5.0.1/lib/active_support/core_ext/hash/transform_values.rb
autocompl-0.1.0 test/dummy/vendor/bundle/ruby/2.3.0/gems/activesupport-5.0.1/lib/active_support/core_ext/hash/transform_values.rb
autocompl-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/activesupport-5.0.1/lib/active_support/core_ext/hash/transform_values.rb
abaci-0.3.0 vendor/bundle/gems/activesupport-5.0.1/lib/active_support/core_ext/hash/transform_values.rb
abaci-0.3.0 vendor/bundle/gems/activesupport-5.0.0/lib/active_support/core_ext/hash/transform_values.rb
activesupport-5.0.1 lib/active_support/core_ext/hash/transform_values.rb
activesupport-5.0.1.rc2 lib/active_support/core_ext/hash/transform_values.rb
activesupport-5.0.1.rc1 lib/active_support/core_ext/hash/transform_values.rb
second_step-0.1.2 secondstep-notify-1.0.0-osx/lib/ruby/lib/ruby/gems/2.2.0/gems/activesupport-5.0.0.1/lib/active_support/core_ext/hash/transform_values.rb
activesupport-5.0.0.1 lib/active_support/core_ext/hash/transform_values.rb
activesupport-5.0.0 lib/active_support/core_ext/hash/transform_values.rb
activesupport-5.0.0.rc2 lib/active_support/core_ext/hash/transform_values.rb
activesupport-5.0.0.racecar1 lib/active_support/core_ext/hash/transform_values.rb
activesupport-5.0.0.rc1 lib/active_support/core_ext/hash/transform_values.rb
activesupport-5.0.0.beta4 lib/active_support/core_ext/hash/transform_values.rb
core_ext-0.0.6 lib/core_ext/hash/transform_values.rb