How to change Hash values?

后端 未结 12 1938
悲&欢浪女
悲&欢浪女 2020-12-12 11:22

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

For example, for given a simple hash:

{\"a\" => \"b\",         


        
12条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-12 12:07

    I do something like this:

    new_hash = Hash[*original_hash.collect{|key,value| [key,value + 1]}.flatten]

    This provides you with the facilities to transform the key or value via any expression also (and it's non-destructive, of course).

提交回复
热议问题