0
0
Rubyprogramming~5 mins

Match operator (=~) in Ruby - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
Anil
B1
C0
Dtrue
What is the result of "dog" =~ /z/?
A0
Bfalse
Cnil
D-1
Which of these is true about the match operator =~?
AReturns index of match or nil
BReturns true or false
CReturns the matched string
DReturns a boolean and the matched string
How can you check if a string contains digits using =~?
Astring =~ /\d/
Bstring =~ /[a-z]/
Cstring =~ /\w/
Dstring =~ /\s/
What does if str =~ /pattern/ do in Ruby?
ANever runs code
BRuns code if pattern is not found
CAlways runs code
DRuns code if pattern is found
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.