简体   繁体   中英

How to iterate or map over a Ruby Hash to change one specific value?

Is there a way iterate or map over the hash below so that you can add the deposit variable to the value 0 of the "john"=>0 key/value pair?

I commented the desired output below.

deposit = 20


Hash = {"Mike"=>0, "John"=>0, "Jen"=>0}


# I would like the result to look like Hash = {"Mike"=>0, "John"=>20, "Jen"=>0}

First, your constant name Hash will mess up many things. Understanding that,

Hash["John"] += deposit

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM