Sha256: 46ca7d3a7478549ad59f3e245be10de0e22d6713555bec0f732df5734df1c32d

Contents?: true

Size: 1015 Bytes

Versions: 29

Compression:

Stored size: 1015 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 unless method_defined? :transform_values

  # 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 unless method_defined? :transform_values!
end

Version data entries

29 entries across 28 versions & 5 rubygems

Version Path
files.com-1.0.55 docs/vendor/bundle/ruby/2.5.0/gems/activesupport-5.0.7.1/lib/active_support/core_ext/hash/transform_values.rb
activesupport-5.0.7.2 lib/active_support/core_ext/hash/transform_values.rb
activesupport-5.0.7.1 lib/active_support/core_ext/hash/transform_values.rb
activesupport-5.0.7 lib/active_support/core_ext/hash/transform_values.rb
activesupport-5.0.6 lib/active_support/core_ext/hash/transform_values.rb
activesupport-5.0.6.rc1 lib/active_support/core_ext/hash/transform_values.rb
activesupport-5.0.5 lib/active_support/core_ext/hash/transform_values.rb
activesupport-5.0.5.rc2 lib/active_support/core_ext/hash/transform_values.rb
activesupport-5.0.5.rc1 lib/active_support/core_ext/hash/transform_values.rb
tdiary-5.0.5 vendor/bundle/gems/activesupport-5.0.2/lib/active_support/core_ext/hash/transform_values.rb
tdiary-5.0.5 vendor/bundle/gems/tdiary-5.0.4/vendor/bundle/gems/activesupport-5.0.2/lib/active_support/core_ext/hash/transform_values.rb
activesupport-5.0.4 lib/active_support/core_ext/hash/transform_values.rb
activesupport-5.0.4.rc1 lib/active_support/core_ext/hash/transform_values.rb
activesupport-5.0.3 lib/active_support/core_ext/hash/transform_values.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/activesupport-5.0.2/lib/active_support/core_ext/hash/transform_values.rb
lazy_record-0.2.1 vendor/bundle/gems/activesupport-5.0.2/lib/active_support/core_ext/hash/transform_values.rb
lazy_record-0.2.0 vendor/bundle/gems/activesupport-5.0.2/lib/active_support/core_ext/hash/transform_values.rb
lazy_record-0.1.9 vendor/bundle/gems/activesupport-5.0.2/lib/active_support/core_ext/hash/transform_values.rb
lazy_record-0.1.8 vendor/bundle/gems/activesupport-5.0.2/lib/active_support/core_ext/hash/transform_values.rb
lazy_record-0.1.7 vendor/bundle/gems/activesupport-5.0.2/lib/active_support/core_ext/hash/transform_values.rb