How to change Hash values?

后端 未结 12 1946
悲&欢浪女
悲&欢浪女 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:15

    This will do it:

    my_hash.each_with_object({}) { |(key, value), hash| hash[key] = value.upcase }
    

    As opposed to inject the advantage is that you are in no need to return the hash again inside the block.

提交回复
热议问题