Forum: Ruby "adding" two hashes

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
C5d520cebf087093e86f29bf4e3ebb63?d=identicon&s=25 Bryson Smith (bryson)
on 2009-01-28 20:09
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!
90a73d9875462aaa9fab2feffafbffe7?d=identicon&s=25 Ben Bleything (Guest)
on 2009-01-28 20:23
(Received via mailing list)
On Wed, Jan 28, 2009 at 11:07 AM, Bryson Smith <bryson@mailinator.com>
wrote:
> Do I need to write my own function to parse through each hash's keys?
> Is there a built-in way to do this?

You can try Hash#merge:

irb(main):007:0> a = {:a => 1}
=> {:a=>1}
irb(main):008:0> b = {:b => 2}
=> {:b=>2}
irb(main):009:0> c = a.merge( b )
=> {:a=>1, :b=>2}

Note that if you have duplicate keys, the values in b will override
the values in a.

Ben
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2009-01-28 20:31
(Received via mailing list)
On Jan 28, 2009, at 2:07 PM, Bryson Smith wrote:

> 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!


irb> hash1 = {:key_one=>100, :key_two=>200}
=> {:key_two=>200, :key_one=>100}
irb> hash2 = {:key_two=>300, :key_three=>400}
=> {:key_two=>300, :key_three=>400}

irb> result = hash1.merge(hash2) {|key,val1,val2| val1+val2}
=> {:key_two=>500, :key_three=>400, :key_one=>100}

You only have to write the block that deals with the duplicates for
'key'

-Rob

Rob Biedenharn    http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
B33ea5c12d767bfd1253940a960274f5?d=identicon&s=25 Tim Hunter (timhunter)
on 2009-01-28 20:33
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?
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2009-01-28 21:13
(Received via mailing list)
On Jan 28, 2009, at 2:31 PM, Tim Hunter wrote:

>>
> 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?

In case it was missed the first time:

irb> result = hash1.merge(hash2) {|key,val1,val2| val1+val2}
=> {:key_two=>500, :key_three=>400, :key_one=>100}

You only have to write the block that deals with the duplicates for
'key'

-Rob

Rob Biedenharn    http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
This topic is locked and can not be replied to.