Recall & Review
beginner
What does the
match method do in Ruby?The
match method searches a string for a pattern using a regular expression and returns a MatchData object if it finds a match, or nil if it doesn't.Click to reveal answer
beginner
What is a
MatchData object in Ruby?A
MatchData object holds details about the match found by the match method, including the matched string, captured groups, and position of the match.Click to reveal answer
intermediate
How can you access the entire matched string from a
MatchData object?You can use
match_data[0] or match_data.to_s to get the full matched string from a MatchData object.Click to reveal answer
intermediate
What does
match_data[1] return?It returns the first captured group from the match, which is the part of the string matched inside the first set of parentheses in the regular expression.
Click to reveal answer
beginner
What will
"hello123".match(/(\d+)/) return?It returns a
MatchData object representing the digits "123" found in the string. You can access it with match_data[0] or match_data[1].Click to reveal answer
What does Ruby's
match method return if no match is found?✗ Incorrect
If the pattern is not found,
match returns nil.How do you get the first captured group from a
MatchData object named m?✗ Incorrect
m[1] returns the first captured group; m[0] is the entire match.Which of these is true about
MatchData?✗ Incorrect
MatchData holds details about the match, including captured groups.What does
"abc123".match(/(\d+)/)[0] return?✗ Incorrect
It returns the matched digits "123" as a string.
If you want to check if a string contains digits using
match, what should you check?✗ Incorrect
A
MatchData object means a match was found; nil means no match.Explain how the
match method works in Ruby and what kind of object it returns.Think about what happens when you look for a pattern in a string.
You got /3 concepts.
Describe how to use a
MatchData object to get the full matched string and captured groups.Remember the numbering starts at zero for the full match.
You got /3 concepts.