Sha256: e756f348d9c7e8c229b8f7430f129654db2742bad40279c8e8f306a7fe1af787

Contents?: true

Size: 601 Bytes

Versions: 42

Compression:

Stored size: 601 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 }
  def transform_values
    return enum_for(:transform_values) unless block_given?
    result = self.class.new
    each do |key, value|
      result[key] = yield(value)
    end
    result
  end

  # Destructive +transform_values+
  def transform_values!
    return enum_for(:transform_values!) unless block_given?
    each do |key, value|
      self[key] = yield(value)
    end
  end
end

Version data entries

42 entries across 40 versions & 8 rubygems

Version Path
activesupport-4.2.7.1 lib/active_support/core_ext/hash/transform_values.rb
activesupport-4.2.7 lib/active_support/core_ext/hash/transform_values.rb
activesupport-4.2.7.rc1 lib/active_support/core_ext/hash/transform_values.rb
ish_lib_manager-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.6/lib/active_support/core_ext/hash/transform_values.rb
activesupport-4.2.6 lib/active_support/core_ext/hash/transform_values.rb
activesupport-4.2.6.rc1 lib/active_support/core_ext/hash/transform_values.rb
activesupport-4.2.5.2 lib/active_support/core_ext/hash/transform_values.rb
activejob-lock-0.0.2 rails/activesupport/lib/active_support/core_ext/hash/transform_values.rb
activesupport-4.2.5.1 lib/active_support/core_ext/hash/transform_values.rb
angular-rails4-templates-0.4.1 vendor/ruby/2.1.0/gems/activesupport-4.2.4/lib/active_support/core_ext/hash/transform_values.rb
angular-rails4-templates-0.4.0 vendor/ruby/2.1.0/gems/activesupport-4.2.4/lib/active_support/core_ext/hash/transform_values.rb
tdiary-4.2.1 vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.5/lib/active_support/core_ext/hash/transform_values.rb
tdiary-4.2.1 vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.5/lib/active_support/core_ext/hash/transform_values.rb
angular-rails4-templates-0.3.0 vendor/ruby/2.1.0/gems/activesupport-4.2.4/lib/active_support/core_ext/hash/transform_values.rb
activesupport-4.2.5 lib/active_support/core_ext/hash/transform_values.rb
activesupport-4.2.5.rc2 lib/active_support/core_ext/hash/transform_values.rb
activesupport-4.2.5.rc1 lib/active_support/core_ext/hash/transform_values.rb
sc_core-0.0.7 test/dummy/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.4/lib/active_support/core_ext/hash/transform_values.rb
activesupport-4.2.4 lib/active_support/core_ext/hash/transform_values.rb
activesupport-4.2.4.rc1 lib/active_support/core_ext/hash/transform_values.rb