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
✗ Incorrect
If the pattern is not found, match returns nil.
How do you get the first captured group from a MatchData object named m?
Am[0]
Bm.group(0)
Cm[1]
Dm.first
✗ Incorrect
m[1] returns the first captured group; m[0] is the entire match.
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
✗ Incorrect
MatchData holds details about the match, including captured groups.
What does "abc123".match(/(\d+)/)[0] return?
A"123"
B"abc"
Cnil
D"abc123"
✗ 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?
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
✗ 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.
Practice
(1/5)
1. What does the Ruby match method return when it finds a pattern in a string?
easy
A. A MatchData object containing details of the match
B. The matched substring as a string
C. An array of all matched substrings
D. A boolean value true
Solution
Step 1: Understand the purpose of match
The match method searches a string for a pattern and returns information about the match.
Step 2: Identify the return type when a match is found
It returns a MatchData object, which holds details like matched text and position.
Final Answer:
A MatchData object containing details of the match -> Option A
Quick Check:
match returns MatchData [OK]
Hint: Remember: match returns MatchData, not just string [OK]
Common Mistakes:
Thinking it returns just the matched string
Assuming it returns an array of matches
Believing it returns true/false
2. Which of the following is the correct syntax to use the match method on a string str to find the pattern /abc/?
easy
A. /abc/.match(str)
B. match(str, /abc/)
C. str.match(/abc/)
D. str.match = /abc/
Solution
Step 1: Recall the method call format
The match method is called on a string object with a regex pattern as argument.
Step 2: Check each option's syntax
str.match(/abc/) correctly calls str.match(/abc/). match(str, /abc/) is invalid syntax. /abc/.match(str) reverses receiver and argument. str.match = /abc/ tries assignment, which is wrong.
Final Answer:
str.match(/abc/) -> Option C
Quick Check:
Correct method call = str.match(/abc/) [OK]
Hint: Call match on string with regex inside parentheses [OK]
Common Mistakes:
Using match as a standalone function
Swapping receiver and argument
Trying to assign with =
3. What will be the output of the following Ruby code?
result = "hello123".match(/\d+/)
puts result[0]
medium
A. 123
B. hello
C. nil
D. An error is raised
Solution
Step 1: Understand the regex pattern
The pattern /\d+/ matches one or more digits in the string.
Step 2: Analyze the match and output
In "hello123", digits "123" match. result[0] accesses the matched substring "123".
Final Answer:
123 -> Option A
Quick Check:
Digits matched = "123" [OK]
Hint: Access matched text with result[0] after match [OK]
Common Mistakes:
Expecting the whole string as output
Confusing nil with empty string
Not using index 0 to get matched text
4. Consider this Ruby code snippet:
text = "abc"
match = text.match(/d/)
puts match[0]
What is the problem here?
medium
A. The regex pattern is invalid
B. Trying to access match[0] when match is nil
C. The string text is empty
D. The match method does not exist for strings
Solution
Step 1: Check the regex pattern and string
The pattern /d/ looks for 'd' in "abc", which is not present, so match returns nil.
Step 2: Identify the error cause
Trying to do match[0] when match is nil causes a NoMethodError.
Final Answer:
Trying to access match[0] when match is nil -> Option B
Quick Check:
Accessing [0] on nil causes error [OK]
Hint: Check if match is nil before accessing [OK]
Common Mistakes:
Assuming pattern is invalid
Ignoring that match can be nil
Thinking match method is missing
5. You want to extract the first word starting with a capital letter from the string "today is a Sunny day" using match. Which code correctly does this and prints the matched word?
hard
A. puts "today is a Sunny day".match(/[A-Z]+/)[0]
B. puts "today is a Sunny day".match(/[a-z]\w*/)[0]
C. puts "today is a Sunny day".match(/\b\w+/)[0]
D. puts "today is a Sunny day".match(/[A-Z]\w*/)[0]
Solution
Step 1: Understand the regex requirements
We want the first word starting with a capital letter. Pattern /[A-Z]\w*/ matches a capital letter followed by zero or more word characters.
Step 2: Analyze each option
puts "today is a Sunny day".match(/[A-Z]\w*/)[0] matches correctly and returns the first capitalized word "Sunny". puts "today is a Sunny day".match(/[a-z]\w*/)[0] matches lowercase start, first is "today", wrong. puts "today is a Sunny day".match(/\b\w+/)[0] matches any word, first is "today", not capitalized. puts "today is a Sunny day".match(/[A-Z]+/)[0] matches only consecutive capital letters, first is "S", not full word.
Final Answer:
puts "today is a Sunny day".match(/[A-Z]\w*/)[0] -> Option D
Quick Check:
Capitalized word match = puts "today is a Sunny day".match(/[A-Z]\w*/)[0] [OK]
Hint: Use regex starting with [A-Z] and access [0] from match [OK]
Common Mistakes:
Using lowercase letter class instead of uppercase
Matching only letters without full word
Not accessing matched text with [0]
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.