Match operator (=~) in Ruby - Time & Space Complexity
We want to understand how the time it takes to check if a pattern matches a string changes as the string gets longer.
How does the match operator (=~) behave when the input grows?
Analyze the time complexity of the following code snippet.
text = "hello world"
pattern = /world/
result = text =~ pattern
puts result
This code checks if the pattern "world" appears in the text string and returns the position if found.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The match operator scans the text string character by character to find the pattern.
- How many times: It may check each character once or more depending on the pattern and text.
As the text gets longer, the match operator may need to check more characters to find the pattern.
| 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 in direct proportion to the length of the text.
Time Complexity: O(n)
This means the time to find a match grows linearly with the size of the text.
[X] Wrong: "The match operator always runs instantly no matter how long the text is."
[OK] Correct: The operator must check characters one by one, so longer text usually means more work.
Understanding how pattern matching scales helps you explain how your code handles larger inputs clearly and confidently.
"What if the pattern was a complex regular expression with repetitions? How would that affect the time complexity?"
Practice
=~ do when used like string =~ /pattern/?Solution
Step 1: Understand the operator's purpose
The=~operator checks if a regex pattern matches anywhere in the string.Step 2: Know the return value
It returns the index of the first match ornilif no match is found.Final Answer:
Returns the index where the pattern starts in the string or nil if not found -> Option DQuick Check:
=~returns index or nil [OK]
- Thinking it returns true or false
- Confusing it with string replacement
- Assuming it splits the string
"hello123" contains digits using the match operator?Solution
Step 1: Identify correct regex syntax
The pattern to match digits is/\d+/, which is a regex literal in Ruby.Step 2: Match operator requires regex, not string
The=~operator expects a regex on the right side, so/\d+/is correct, not a string.Final Answer:
"hello123" =~ /\d+/ -> Option AQuick Check:
Regex literal with =~ is correct [OK]
- Using a string instead of regex on right side
- Confusing \d with \w for digits
- Missing regex delimiters
text = "OpenAI GPT-4" index = text =~ /GPT/ puts index
Solution
Step 1: Locate the pattern in the string
The string is "OpenAI GPT-4". The substring "GPT" starts at index 7 (counting from 0).Step 2: Understand what =~ returns
The operator returns the index of the first match, so it returns 7.Final Answer:
7 -> Option CQuick Check:
Index of "GPT" in string = 7 [OK]
- Counting from 1 instead of 0
- Expecting true/false instead of index
- Assuming nil if unsure
str = "test123" result = str =~ "\d+" puts result
Solution
Step 1: Check the right side of =~ operator
The code uses a string "\d+" instead of a regex literal /\d+/.Step 2: Understand =~ expects regex
The match operator requires a regex pattern, so using a string causes a TypeError or unexpected behavior.Final Answer:
Using a string instead of a regex pattern with =~ -> Option AQuick Check:
Right side of =~ must be regex [OK]
- Using strings instead of regex
- Thinking parentheses are needed
- Confusing puts with print
words = ["cat", "dog", "bird", "cow"], which code snippet uses the match operator to find the first word containing the letter 'i' and returns its index in the array?Solution
Step 1: Understand the goal
We want the index of the first word containing 'i'.Step 2: Use find_index with match operator
find_indexreturns the index of the first element where the block is true. Usingw =~ /i/returns index or nil, so truthy means match found.Step 3: Check other options
words.index { |w| w.include?('i') } usesinclude?which works but is not using the match operator. words.find { |w| w =~ /i/ } returns the word, not index. words.select { |w| w =~ /i/ } returns all matches, not index.Final Answer:
words.find_index { |w| w =~ /i/ } -> Option BQuick Check:
find_index with =~ returns correct index [OK]
- Using find instead of find_index for index
- Using include? instead of =~
- Expecting select to return single index
Ready to prove it?
Pass the test and this pattern moves unknown
