0

I start with a basic hash where the key is a string and value an integer.

hash = {"a"=>2, "b"=>3}

Then what I try to achieve is that i want to push several times into that hash a new hash with different keys or / and same :

hash2 = {"c"=>4, "a"=>5}

The result should be

h_result = {"a"=>7, "b"=>3, "c"=>4}

The first thing would be to push the new hash and keep the duplicate keys. I saw that answer = How can I merge two hashes without overwritten duplicate keys in Ruby? but it seems that it's not working..

Then I think I should match the same keys and compute the values. But again I can't find the answer.

Thanks guys

  • I'm not sure what the specific issue is--the simplest thing to do is to iterate over the incoming hash's key/value pairs, if the key is found in the original hash, set the new value to existing + incoming, if it isn't, add the incoming key/value pair. Can you be more clear about what the problem is? – Dave Newton Nov 20 '18 at 15:27
  • 2
    this question has been asked a time or two https://stackoverflow.com/questions/4091487/sum-2-hashes-attributes-with-the-same-key, https://stackoverflow.com/questions/4453511/group-hashes-by-keys-and-sum-the-values, https://stackoverflow.com/questions/48868886/merge-and-sum-hashes-inside-a-ruby-array, https://stackoverflow.com/questions/36004998/add-values-of-same-key-in-array-of-hashes, https://stackoverflow.com/questions/37003362/array-with-hash-how-to-merge-same-keys-and-add-its-value – engineersmnky Nov 20 '18 at 15:31
  • Hello @DaveNewton what i want to do is the simplest thing you're talking about, i just don't know how to code it. – TARK WAGON Nov 20 '18 at 15:32
  • Possible duplicate of [Sum 2 hashes attributes with the same key](https://stackoverflow.com/questions/4091487/sum-2-hashes-attributes-with-the-same-key) – anothermh Nov 20 '18 at 17:18

2 Answers2

1

If you just want to compute equal keys in the hash what you are looking for is the merge method in the Hash class.

https://ruby-doc.org/core-2.2.1/Hash.html#method-i-merge

Returns a new hash containing the contents of other_hash and the contents of hsh. If no block is specified, the value for entries with duplicate keys will be that of other_hash. Otherwise the value for each duplicate key is determined by calling the block with the key, its value in hsh and its value in other_hash.

When you pass a block to the merge method it will yield both old value and new value, and the you can do your computation there.

For instance:

hash  = {"a"=>2, "b"=>3}
hash2 = {"c"=>4, "a"=>5}

result = hash.merge(hash2) { |key, old_val, new_val| old_val + new_val }

p result #=> {"a"=>7, "b"=>3, "c"=>4}
Jonathan Duarte
  • 753
  • 5
  • 9
1

Just use Hash#merge with a block and tell Ruby what to do when the key exists in both hashes – in this example just add the value from the second hash to the value from the first hash.

hash.merge(hash2) { |key, v1, v2| v1 + v2 }
#=> { "a" => 7, "b" => 3, "c" => 4 }
spickermann
  • 100,941
  • 9
  • 101
  • 131