Bird
0
0

Which of the following is the correct syntax to update hash a with hash b in Ruby?

easy📝 Syntax Q12 of 15
Ruby - Hashes

Which of the following is the correct syntax to update hash a with hash b in Ruby?

a = {x: 1}
b = {y: 2}
A<code>a + b</code>
B<code>a.merge!b</code>
C<code>a.merge b</code>
D<code>a.update(b)</code>
Step-by-Step Solution
Solution:
  1. Step 1: Check syntax for updating hash

    update is a method that modifies the original hash and takes another hash as argument with parentheses.
  2. Step 2: Compare with other options

    merge! also updates but usually called with ! and parentheses are optional but update(b) is the most explicit and correct syntax here.
  3. Final Answer:

    a.update(b) -> Option D
  4. Quick Check:

    Use update() to modify original hash [OK]
Quick Trick: Use update(hash) to change original hash [OK]
Common Mistakes:
MISTAKES
  • Using + operator to combine hashes (not valid)
  • Forgetting parentheses in method call
  • Confusing merge and update syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes