From: Tim Hunter Date: 2009-01-29T04:31:00+09:00 Subject: Re: "adding" two hashes Bryson Smith wrote: > Hi, > > I'm wondering if it is possible to "add" the values of two hashes > together. Suppose I had: > > hash1 = {:key_one=>100, :key_two=>200} > hash2 = {:key_two=>300, :key_three=>400} > > 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? -- Posted via http://www.ruby-forum.com/.