Ruby: change each value in a hash with something like #collect for arrays?

Posted by Adam Nonymous on Stack Overflow See other posts from Stack Overflow or by Adam Nonymous
Published on 2009-05-01T18:17:45Z Indexed on 2010/03/27 14:23 UTC
Read the original article Hit count: 312

Filed under:
|
|
|

Hi!

I'd like to replace each value in a hash with value.some_method.

For example in 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?

Thanks in advance,

Adam Nonymous

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 {...} ;)

© Stack Overflow or respective owner

Related posts about ruby

Related posts about hash