0
0
Rubyprogramming~10 mins

Named captures in Ruby - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Named captures
Define regex with named captures
Apply regex to string
Match found?
NoExit: No match
Yes
Extract named groups
Use captured values
The program defines a regex with named groups, applies it to a string, checks for a match, and extracts the named captures for use.
Execution Sample
Ruby
text = "Name: Alice, Age: 30"
regex = /Name: (?<name>\w+), Age: (?<age>\d+)/
match = regex.match(text)
puts match[:name]
puts match[:age]
This code extracts the name and age from a string using named captures in a regex.
Execution Table
StepActionEvaluationResult
1Define text stringtext = "Name: Alice, Age: 30"text holds the string
2Define regex with named captures/Name: (?<name>\w+), Age: (?<age>\d+)/regex holds pattern with 'name' and 'age' groups
3Apply regex.match(text)regex.match(text)match object with captures if matched
4Check if match is foundmatch != niltrue (match found)
5Extract match[:name]match[:name]"Alice"
6Extract match[:age]match[:age]"30"
7Print match[:name]puts match[:name]Outputs: Alice
8Print match[:age]puts match[:age]Outputs: 30
💡 Execution ends after printing captured values from named groups.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5Final
textnil"Name: Alice, Age: 30""Name: Alice, Age: 30""Name: Alice, Age: 30""Name: Alice, Age: 30""Name: Alice, Age: 30""Name: Alice, Age: 30"
regexnilnil/Name: (?<name>\w+), Age: (?<age>\d+)//Name: (?<name>\w+), Age: (?<age>\d+)//Name: (?<name>\w+), Age: (?<age>\d+)//Name: (?<name>\w+), Age: (?<age>\d+)//Name: (?<name>\w+), Age: (?<age>\d+)/
matchnilnilnil#<MatchData "Name: Alice, Age: 30" name:"Alice" age:"30">#<MatchData "Name: Alice, Age: 30" name:"Alice" age:"30">#<MatchData "Name: Alice, Age: 30" name:"Alice" age:"30">#<MatchData "Name: Alice, Age: 30" name:"Alice" age:"30">
match[:name]nilnilnilnil"Alice""Alice""Alice"
match[:age]nilnilnilnilnil"30""30"
Key Moments - 2 Insights
Why do we use match[:name] instead of match[1] to get the captured value?
Using match[:name] accesses the captured group by its name, which is clearer and less error-prone than using numeric indexes like match[1]. This is shown in execution_table rows 5 and 6 where named captures are extracted.
What happens if the regex does not match the string?
If no match is found, match will be nil (see execution_table step 4). Trying to access match[:name] would cause an error, so always check if match is not nil before accessing captures.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 5, what is the value of match[:name]?
A"Alice"
B"30"
Cnil
D"Name"
💡 Hint
Check the 'Result' column at step 5 in the execution_table.
At which step does the program confirm a successful match?
AStep 3
BStep 4
CStep 6
DStep 2
💡 Hint
Look at the 'Action' and 'Evaluation' columns in execution_table to find where match != nil is checked.
If the input string was "Name: Bob, Age: 25", what would match[:age] be at step 6?
A"Alice"
Bnil
C"25"
D"Bob"
💡 Hint
Refer to variable_tracker for match[:age] values and imagine the string changed to "Name: Bob, Age: 25".
Concept Snapshot
Named captures in Ruby regex use syntax (?<name>pattern).
Apply regex.match(string) to get MatchData.
Access groups by name: match[:name].
Check match is not nil before accessing.
Makes code clearer and safer than numeric indexes.
Full Transcript
This example shows how Ruby uses named captures in regular expressions. First, a string with a name and age is defined. Then, a regex with named groups 'name' and 'age' is created. The regex is applied to the string using match. If a match is found, the named captures are extracted using match[:name] and match[:age]. These values are printed. The execution table traces each step, showing variable values and actions. Key points include using named captures for clarity and checking for a match before accessing captures. The visual quiz tests understanding of these steps and values.