instance method
Hash#transform_values
() -> ::Enumerator[V, ::Hash[K, untyped]]
[A] () { (V) -> A } -> ::Hash[K, A]
transform_values {|value| ... } → new_hash
transform_values → new_enumerator
hash.c
View on GitHub ↗
static VALUE
rb_hash_transform_values(VALUE hash)
{
VALUE result;
RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size);
result = hash_dup_with_compare_by_id(hash);
SET_DEFAULT(result, Qnil);
if (!RHASH_EMPTY_P(hash)) {
transform_values(result);
compact_after_delete(result);
}
return result;
}
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.
See also
#transform_values!
#transform_keys!
#transform_keys
#update
also in
4.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0