Recall & Review
beginner
What does the
merge method do in Ruby hashes?It combines two hashes into a new hash. If keys overlap, the values from the second hash overwrite those from the first. The original hashes stay unchanged.
Click to reveal answer
beginner
How does
merge! differ from merge in Ruby?merge! updates the original hash in place by adding or replacing keys and values from another hash, while merge returns a new hash without changing the original.Click to reveal answer
beginner
What happens if two hashes have the same key when using
merge?The value from the second hash replaces the value from the first hash for that key in the resulting hash.
Click to reveal answer
beginner
Can you provide a simple example of using
merge in Ruby?Yes! For example:<br>
h1 = {a: 1, b: 2}
h2 = {b: 3, c: 4}
h3 = h1.merge(h2)
# h3 is {:a=>1, :b=>3, :c=>4}Click to reveal answer
intermediate
What is the return value of
merge!?merge! returns the updated original hash after merging the other hash into it.Click to reveal answer
What does
hash1.merge(hash2) do in Ruby?✗ Incorrect
The
merge method returns a new hash combining both hashes, with values from the second hash overwriting duplicates.Which method modifies the original hash in Ruby?
✗ Incorrect
merge! modifies the original hash by merging another hash into it.If
h1 = {x: 1} and h2 = {x: 2}, what is h1.merge(h2)?✗ Incorrect
The value for key :x from h2 overwrites the one from h1 in the merged hash.
What does
merge! return after execution?✗ Incorrect
merge! returns the original hash after it has been updated.Which of these is true about
merge and merge!?✗ Incorrect
merge returns a new hash without changing the original, while merge! updates the original hash.Explain how the
merge and merge! methods work in Ruby hashes.Think about whether the original hash changes or not.
You got /4 concepts.
Describe a situation where you would prefer to use
merge over merge!.Consider if you want to keep your original data intact.
You got /4 concepts.