0
0
Rubyprogramming~5 mins

Merge and update methods in Ruby - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
AReturns nil
BUpdates hash1 in place with hash2's keys and values
CDeletes keys from hash1 that are in hash2
DReturns a new hash combining both, with hash2's values overwriting duplicates
Which method modifies the original hash in Ruby?
Amerge!
Bmerge
Cdup
Dselect
If h1 = {x: 1} and h2 = {x: 2}, what is h1.merge(h2)?
A{x: 2}
B{x: 1, x: 2}
C{x: 1}
DError
What does merge! return after execution?
AA new hash
BThe original hash updated
CAn array of keys
Dnil
Which of these is true about merge and merge!?
A<code>merge</code> changes the original hash, <code>merge!</code> does not
BBoth change the original hash
C<code>merge</code> returns a new hash, <code>merge!</code> changes the original hash
DNeither changes 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.