0
0
Rubyprogramming~5 mins

Match method and MatchData in Ruby - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
AAn empty MatchData object
BAn empty string
Cfalse
Dnil
How do you get the first captured group from a MatchData object named m?
Am[0]
Bm.group(0)
Cm[1]
Dm.first
Which of these is true about MatchData?
AIt stores the matched string and captured groups
BIt is a string
CIt is returned by <code>String#split</code>
DIt is a boolean value
What does "abc123".match(/(\d+)/)[0] return?
A"123"
B"abc"
Cnil
D"abc123"
If you want to check if a string contains digits using match, what should you check?
AIf <code>match</code> returns an empty string
BIf <code>match</code> returns a <code>MatchData</code> object
CIf <code>match</code> returns false
DIf <code>match</code> returns zero
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.