Bird
0
0

Identify the problem in this Ruby code:

medium📝 Debug Q7 of 15
Ruby - Regular Expressions
Identify the problem in this Ruby code:
pattern = /abc/
text = "xyzabc"
if text =~ pattern
  puts "Match found"
else
  puts "No match"
end
Atext should be a regex, not string
Bpattern should be a string, not regex
CNo problem, code runs correctly
DUsing == instead of =~ in if
Step-by-Step Solution
Solution:
  1. Step 1: Review variable types and usage

    pattern is a regex, text is a string, which is correct for =~.
  2. Step 2: Check the if condition

    text =~ pattern correctly checks for a match and prints accordingly.
  3. Final Answer:

    No problem, code runs correctly -> Option C
  4. Quick Check:

    Regex variable with =~ works fine [OK]
Quick Trick: Regex variables can be used with =~ operator [OK]
Common Mistakes:
  • Thinking regex must be inline
  • Confusing string and regex roles
  • Replacing =~ with ==

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes