Bird
0
0

Consider this Ruby code snippet:

medium📝 Debug Q14 of 15
Ruby - Regular Expressions
Consider this Ruby code snippet:
text = "abc"
match = text.match(/d/)
puts match[0]
What is the problem here?
AThe regex pattern is invalid
BTrying to access <code>match[0]</code> when <code>match</code> is nil
CThe string <code>text</code> is empty
DThe <code>match</code> method does not exist for strings
Step-by-Step Solution
Solution:
  1. Step 1: Check the regex pattern and string

    The pattern /d/ looks for 'd' in "abc", which is not present, so match returns nil.
  2. Step 2: Identify the error cause

    Trying to do match[0] when match is nil causes a NoMethodError.
  3. Final Answer:

    Trying to access match[0] when match is nil -> Option B
  4. Quick Check:

    Accessing [0] on nil causes error [OK]
Quick Trick: Check if match is nil before accessing [OK]
Common Mistakes:
  • Assuming pattern is invalid
  • Ignoring that match can be nil
  • Thinking match method is missing

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes