Bird
0
0

What will be the output of this Ruby code?

medium📝 Predict Output Q5 of 15
Ruby - Regular Expressions
What will be the output of this Ruby code?
result = "test123" =~ /\D+/ 
puts result
A0
B4
Cnil
DError
Step-by-Step Solution
Solution:
  1. Step 1: Understand the regex pattern

    /\D+/ matches one or more non-digit characters.
  2. Step 2: Find first non-digit match in "test123"

    The substring "test" at index 0 matches the pattern.
  3. Final Answer:

    0 -> Option A
  4. Quick Check:

    Non-digit match starts at index 0 [OK]
Quick Trick: \D+ matches non-digits starting at index 0 [OK]
Common Mistakes:
  • Expecting index 4 where digits start
  • Confusing \D with \d
  • Expecting nil if digits exist

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes