End of Life Ruby 3.2 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
instance method
Hash#transform_values!
hash.transform_values! {|value| ... } → self
hash.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)) {
rb_hash_stlike_foreach_with_replace(hash, transform_values_foreach_func, transform_values_foreach_replace, hash);
}
return hash;
}
Returns self, whose keys are unchanged, and whose values are determined by the given block.
Example
h = {foo: 0, bar: 1, baz: 2} h.transform_values! {|value| value * 100} # => {:foo=>0, :bar=>100, :baz=>200}
Returns a new Enumerator if no block given:
Example
h = {foo: 0, bar: 1, baz: 2} e = h.transform_values! # => #<Enumerator: {:foo=>0, :bar=>100, :baz=>200}:transform_values!> h1 = e.each {|value| value * 100} h1 # => {:foo=>0, :bar=>100, :baz=>200}
See also
#transform_values
#update
#transform_keys!
#value?
also in
4.1 · 4.0 · 3.4 · 3.3 · 3.1 · 3.0