Bird
0
0

What is the output of this Ruby code?

medium📝 Predict Output Q4 of 15
Ruby - Regular Expressions
What is the output of this Ruby code?
text = 'Order number: 12345'
match = text.match(/number: (\d+)/)
puts match[1]
AError
Bnumber: 12345
Cnil
D12345
Step-by-Step Solution
Solution:
  1. Step 1: Understand the regex and match

    The regex captures digits (\d+) after 'number: ' in a capture group.
  2. Step 2: Extract captured group

    match[1] returns the first capture group, which is '12345'.
  3. Final Answer:

    12345 -> Option D
  4. Quick Check:

    Capture group returns digits '12345' [OK]
Quick Trick: Use \d+ to capture digits in regex [OK]
Common Mistakes:
  • Accessing match[0] which returns full match
  • Forgetting to escape backslash in regex
  • Assuming match returns nil if no capture group

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes