Bird
0
0

What does the Ruby operator =~ do when used like string =~ /pattern/?

easy📝 Conceptual Q11 of 15
Ruby - Regular Expressions
What does the Ruby operator =~ do when used like string =~ /pattern/?
ASplits the string into an array based on the pattern
BReturns true if the pattern is found, false otherwise
CReplaces the pattern in the string with another string
DReturns the index where the pattern starts in the string or nil if not found
Step-by-Step Solution
Solution:
  1. Step 1: Understand the operator's purpose

    The =~ operator checks if a regex pattern matches anywhere in the string.
  2. Step 2: Know the return value

    It returns the index of the first match or nil if no match is found.
  3. Final Answer:

    Returns the index where the pattern starts in the string or nil if not found -> Option D
  4. Quick Check:

    =~ returns index or nil [OK]
Quick Trick: Remember: =~ returns index or nil, not true/false [OK]
Common Mistakes:
  • Thinking it returns true or false
  • Confusing it with string replacement
  • Assuming it splits the string

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes