Match method and MatchData in Ruby - Time & Space Complexity
We want to understand how long it takes for Ruby's match method to find a pattern in a string.
How does the time needed grow when the string gets bigger?
Analyze the time complexity of the following code snippet.
text = "hello world"
pattern = /world/
result = text.match(pattern)
if result
puts "Found: #{result[0]}"
else
puts "Not found"
end
This code tries to find the word "world" inside the string "hello world" using the match method.
Look for parts that repeat or check many characters.
- Primary operation: Checking each character in the string to see if the pattern fits.
- How many times: Up to once for each character in the string, until the pattern is found or the string ends.
As the string gets longer, the method checks more characters.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks |
| 100 | About 100 checks |
| 1000 | About 1000 checks |
Pattern observation: The number of checks grows roughly the same as the string length.
Time Complexity: O(n)
This means the time to find the pattern grows in a straight line with the string size.
[X] Wrong: "The match method always takes the same time no matter the string size."
[OK] Correct: The method checks characters one by one, so longer strings usually take more time.
Understanding how pattern matching grows with input size helps you explain how your code handles bigger data smoothly.
"What if the pattern was a more complex regular expression? How would that affect the time complexity?"
Practice
match method return when it finds a pattern in a string?Solution
Step 1: Understand the purpose of
Thematchmatchmethod 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 aMatchDataobject, which holds details like matched text and position.Final Answer:
AMatchDataobject containing details of the match -> Option AQuick Check:
matchreturnsMatchData[OK]
match returns MatchData, not just string [OK]- Thinking it returns just the matched string
- Assuming it returns an array of matches
- Believing it returns true/false
match method on a string str to find the pattern /abc/?Solution
Step 1: Recall the method call format
Thematchmethod is called on a string object with a regex pattern as argument.Step 2: Check each option's syntax
str.match(/abc/) correctly callsstr.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 CQuick Check:
Correct method call = str.match(/abc/) [OK]
match on string with regex inside parentheses [OK]- Using
matchas a standalone function - Swapping receiver and argument
- Trying to assign with
=
result = "hello123".match(/\d+/) puts result[0]
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 AQuick Check:
Digits matched = "123" [OK]
result[0] after match [OK]- Expecting the whole string as output
- Confusing
nilwith empty string - Not using index 0 to get matched text
text = "abc" match = text.match(/d/) puts match[0]What is the problem here?
Solution
Step 1: Check the regex pattern and string
The pattern/d/looks for 'd' in "abc", which is not present, somatchreturnsnil.Step 2: Identify the error cause
Trying to domatch[0]whenmatchisnilcauses aNoMethodError.Final Answer:
Trying to accessmatch[0]whenmatchis nil -> Option BQuick Check:
Accessing[0]on nil causes error [OK]
match is nil before accessing [OK]- Assuming pattern is invalid
- Ignoring that
matchcan be nil - Thinking
matchmethod is missing
"today is a Sunny day" using match. Which code correctly does this and prints the matched word?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 DQuick Check:
Capitalized word match = puts "today is a Sunny day".match(/[A-Z]\w*/)[0] [OK]
- Using lowercase letter class instead of uppercase
- Matching only letters without full word
- Not accessing matched text with [0]
Ready to prove it?
Pass the test and this pattern moves unknown
