Bird
0
0

In Ruby DSL building, what happens if you define multiple keys with the same name in a hash inside the DSL block?

hard📝 Conceptual Q10 of 15
Ruby - Advanced Metaprogramming
In Ruby DSL building, what happens if you define multiple keys with the same name in a hash inside the DSL block?
AThe first key-value pair is preserved, others ignored
BAn error is raised for duplicate keys
CAll values are stored as an array under the key
DThe last key-value pair overwrites previous ones
Step-by-Step Solution
Solution:
  1. Step 1: Recall Ruby hash behavior

    Ruby hashes overwrite earlier values when duplicate keys are assigned.
  2. Step 2: Apply to DSL context

    DSL hashes behave the same; last key-value pair wins silently.
  3. Final Answer:

    The last key-value pair overwrites previous ones -> Option D
  4. Quick Check:

    Duplicate hash keys overwrite previous values [OK]
Quick Trick: Duplicate keys in hashes overwrite earlier entries silently [OK]
Common Mistakes:
  • Expecting error on duplicate keys
  • Assuming values accumulate automatically
  • Thinking first key is preserved

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes