I’d like to “add” the hashes together so that I get
result => {:key_one=>100, :key_two=>500, :key_three=>400}
Do I need to write my own function to parse through each hash’s keys?
Is there a built-in way to do this?
Thanks!
No built-in way, but it would be easy to use hash1.each_key to iterate
over the keys of hash1. For every key in hash1, get its value for that
key from hash1 and hash2, add the two values and store the sum as the
value of that key in result.
The question is, what happens if there’s a key in hash1 that isn’t in
hash2, or vice versa?
Yeah it’s awesome but i am struggle with my case if you have any ideas please share to me i am very new to ruby … OK let’s explain my problem. i have a two hashes i want to merge two hashes my hashes like key pair value, But values is also hash type so only struggle in this case a = {"Additional Options"=>{"Break Fast"=>100.0,"Lunch"=>300.0}, "Discount"=>{}, "Coupon"=>{"cou"=>10}} b = {"Additional Options"=>{"Break Fast"=>100.0}, "Discount"=>{}, "Coupon"=>{"cou"=>10.0}}
i am trying to merge a.merge(b){ |k,v1,v2|v1+v2} i know this wrong but really i dont no how do this please me anyone…
My Expectation is Like This {"Additional Options"=>{"Break Fast"=>200.0,"Lunch"=>300.0}, "Discount"=>{}, "Coupon"=>{"cou"=>10.0}
Thanks Advance Developers…