Bird
0
0

What does the Ruby match operator =~ return when the pattern is found in the string?

easy📝 Conceptual Q1 of 15
Ruby - Regular Expressions
What does the Ruby match operator =~ return when the pattern is found in the string?
AThe index of the first match
BTrue
CFalse
DThe matched substring
Step-by-Step Solution
Solution:
  1. Step 1: Understand the match operator behavior

    The =~ operator returns the index position of the first match in the string if found.
  2. Step 2: Compare with other possible return values

    It does not return true/false or the matched substring directly.
  3. Final Answer:

    The index of the first match -> Option A
  4. Quick Check:

    Match operator returns index [OK]
Quick Trick: Match operator returns index, not boolean or substring [OK]
Common Mistakes:
  • Thinking it returns true or false
  • Expecting the matched substring as return
  • Confusing with equality operator

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes