method

transform_values

Importance_1
Ruby latest stable (v2_5_5) - 0 notes - Class: Hash
transform_values() public

Returns a new hash with the results of running the block once for every value. This method does not change the keys.

h = { a: 1, b: 2, c: 3 }
h.transform_values {|v| v * v + 1 }  #=> { a: 2, b: 5, c: 10 }
h.transform_values(&:to_s)           #=> { a: "1", b: "2", c: "3" }
h.transform_values.with_index {|v, i| "#{v}.#{i}" }
                                     #=> { a: "1.0", b: "2.1", c: "3.2" }

If no block is given, an enumerator is returned instead.

Show source
Register or log in to add new notes.