instance method transform_values

Ruby on Rails 5.2.8.1

Since v4.2.9 Last seen in v5.2.8.1

Available in: v4.2.9 v5.2.8.1

Signature

transform_values()

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 }
Source
# File activesupport/lib/active_support/core_ext/hash/transform_values.rb, line 13
  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

Defined in activesupport/lib/active_support/core_ext/hash/transform_values.rb line 13 · View on GitHub · Improve this page · Find usages on GitHub

Defined in Hash

Type at least 2 characters to search.

↑↓ navigate · open · esc close