Bird
0
0

What happens if you assign a value to a hash key that already exists in Ruby?

hard📝 Conceptual Q10 of 15
Ruby - Hashes
What happens if you assign a value to a hash key that already exists in Ruby?
AThe new value is ignored
BAn error is raised
CThe old value is replaced with the new value
DBoth old and new values are stored in an array
Step-by-Step Solution
Solution:
  1. Step 1: Understand hash key assignment behavior

    Assigning a value to an existing key replaces the old value.
  2. Step 2: Confirm no error or array storage occurs

    Ruby hashes overwrite old values silently without error or storing multiple values.
  3. Final Answer:

    The old value is replaced with the new value -> Option C
  4. Quick Check:

    Hash key assignment replaces old value [OK]
Quick Trick: Assigning existing key replaces old value in hash [OK]
Common Mistakes:
  • Expecting error on duplicate key assignment
  • Thinking values accumulate in arrays
  • Assuming new value is ignored

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes