Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

instance method

Hash#transform_values

() -> ::Enumerator[V, ::Hash[K, untyped]]
[V2] () { (V value) -> V2 } -> ::Hash[K, V2]
transform_values {|value| ... } → new_hash
transform_values → new_enumerator

With a block given, returns a new hash new_hash; for each pair key/value in self, calls the block with value and captures its return as new_value; adds to new_hash the entry key/new_value:

Example
h = {foo: 0, bar: 1, baz: 2}
h1 = h.transform_values {|value| value * 100}
h1 # => {foo: 0, bar: 100, baz: 200}

With no block given, returns a new Enumerator.

Related: see Methods for Transforming Keys and Values.