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:
Step 1: Identify method returning named captures hash
String#match returns a MatchData object which has a named_captures method returning a hash.
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.
Final Answer:
String#match -> Option B
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
Master "Regular Expressions" in Ruby
9 interactive learning modes - each teaches the same concept differently