Ruby - Hashes
You have two hashes representing inventory counts:
stock1 = {apples: 10, oranges: 5}stock2 = {oranges: 7, bananas: 3}How can you create a new hash that sums the counts for overlapping keys?
You have two hashes representing inventory counts:
stock1 = {apples: 10, oranges: 5}stock2 = {oranges: 7, bananas: 3}How can you create a new hash that sums the counts for overlapping keys?
merge with a block lets you define how to combine values for duplicate keys.{ |key, old, new| old + new } sums counts for overlapping keys like :oranges.15+ quiz questions · All difficulty levels · Free
Free Signup - Practice All Questions