I'd like to replace each value
in a hash with value.some_method
.
For example, for given a simple hash:
{"a" => "b", "c" => "d"}`
every value should be .upcase
d, so it looks like:
{"a" => "B", "c" => "D"}
I tried #collect
and #map
but always just get arrays back. Is there an elegant way to do this?
UPDATE
Damn, I forgot: The hash is in an instance variable which should not be changed. I need a new hash with the changed values, but would prefer not to define that variable explicitly and then loop over the hash filling it. Something like:
new_hash = hash.magic{ ... }
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…