Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialBanana Eatsmoney
1,333 PointsIs #merge same as adding new key and values?
Seems like the merge method is also just adding a new pair of key and value to the original hash?
What's the difference, if any?
1 Answer
Maciej Czuchnowski
36,441 PointsIn short - it RETURNS a new hash that is a combination of the first hash and the hash given as an argument. So it's not exactly adding keys/values, since you're not modifying anything. Both hashes (if they were kept in separate variables) still have the same contents. There is, however, a #merge!
method that will modify the first hash, so it literally adds values and keys from the second hash to the first one:
https://ruby-doc.org/core-2.2.0/Hash.html#method-i-merge-21