Bird
0
0

Why does Ruby's named captures use symbols as keys in MatchData instead of strings?

hard📝 Conceptual Q10 of 15
Ruby - Regular Expressions
Why does Ruby's named captures use symbols as keys in MatchData instead of strings?
ASymbols are more memory-efficient and faster for repeated keys.
BStrings cannot be used as keys in Ruby hashes.
CSymbols automatically convert to integers internally.
DUsing strings would cause syntax errors in regex.
Step-by-Step Solution
Solution:
  1. Step 1: Understand Ruby symbols vs strings

    Symbols are immutable and reused, making them efficient as hash keys.
  2. Step 2: Eliminate incorrect options

    Strings can be hash keys; symbols do not convert to integers; strings do not cause regex syntax errors.
  3. Final Answer:

    Symbols are more memory-efficient and faster for repeated keys. -> Option A
  4. Quick Check:

    Symbols preferred for keys due to efficiency [OK]
Quick Trick: Symbols save memory and speed up key lookups [OK]
Common Mistakes:
  • Thinking strings can't be hash keys
  • Believing symbols convert to numbers
  • Confusing regex syntax with hash key types

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes