Bird
0
0

Which Ruby method returns a hash of named captures after a regex match?

easy📝 Conceptual Q2 of 15
Ruby - Regular Expressions
Which Ruby method returns a hash of named captures after a regex match?
ARegexp#named_captures
BString#match
CString#scan
DRegexp#captures
Step-by-Step Solution
Solution:
  1. Step 1: Identify method returning named captures hash

    String#match returns a MatchData object which has a named_captures method returning a hash.
  2. Step 2: Eliminate other options

    String#scan returns arrays of matches, Regexp#named_captures returns a hash of group names to patterns, not match results, Regexp#captures returns array of captures without names.
  3. Final Answer:

    String#match -> Option B
  4. Quick Check:

    Named captures hash = String#match [OK]
Quick Trick: Use String#match to get named captures hash [OK]
Common Mistakes:
  • Using String#scan expecting named captures hash
  • Confusing Regexp#named_captures with match results
  • Expecting Regexp#captures to return named keys

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes