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, self]
() { (V value) -> V } -> self
transform_values! {|old_value| ... } → self
transform_values! → new_enumerator
hash.c
View on GitHub ↗
static VALUE
rb_hash_transform_values_bang(VALUE hash)
{
RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size);
rb_hash_modify_check(hash);
if (!RHASH_TABLE_EMPTY_P(hash)) {
transform_values(hash);
}
return hash;
}
With a block given, changes the values of self as determined by the block; returns self.
For each entry key/old_value in self, calls the block with old_value, captures its return value as new_value, and sets self[key] = new_value:
Example
h = {foo: 0, bar: 1, baz: 2} h.transform_values! {|value| value * 100} # => {foo: 0, bar: 100, baz: 200}
With no block given, returns a new Enumerator.
Related: see Methods for Transforming Keys and Values.
See also
#transform_values
#update
#transform_keys!
#value?
also in
4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0