Recall & Review
beginner
What does the Ruby match operator
=~ do?It checks if a string matches a pattern defined by a regular expression. It returns the index of the first match or
nil if no match is found.Click to reveal answer
beginner
What is the return value of
"hello" =~ /e/ in Ruby?It returns
1 because the letter 'e' is found at index 1 in the string "hello".Click to reveal answer
beginner
What does the match operator
=~ return if there is no match?It returns
nil, which means no match was found in the string.Click to reveal answer
beginner
How can you use the match operator
=~ in an if statement?You can write
if string =~ /pattern/ to check if the pattern exists in the string. If it matches, the condition is true; otherwise, false.Click to reveal answer
intermediate
What is the difference between
=~ and match method in Ruby?=~ returns the index of the match or nil. The match method returns a MatchData object with detailed info about the match.Click to reveal answer
What does
"cat" =~ /a/ return in Ruby?✗ Incorrect
The letter 'a' is at index 1 in "cat", so the match operator returns 1.
What is the result of
"dog" =~ /z/?✗ Incorrect
No 'z' in "dog", so the match operator returns nil.
Which of these is true about the match operator
=~?✗ Incorrect
It returns the index where the match starts or nil if no match.
How can you check if a string contains digits using
=~?✗ Incorrect
The regex /\d/ matches any digit character.
What does
if str =~ /pattern/ do in Ruby?✗ Incorrect
The condition is true if the pattern matches the string.
Explain how the Ruby match operator
=~ works with strings and regular expressions.Think about what happens when you look for a pattern inside a word.
You got /3 concepts.
Describe how you would use the match operator
=~ inside an if statement to check for a pattern.Remember how Ruby treats nil in conditions.
You got /3 concepts.