Bird
0
0

What is the output of this Ruby code?

medium📝 Predict Output Q13 of 15
Ruby - Regular Expressions
What is the output of this Ruby code?
text = "Room 101"
match = text.match(/\d+/)
puts match[0]
A101
BRoom
C1
Dnil
Step-by-Step Solution
Solution:
  1. Step 1: Understand the regex /\d+/

    This pattern matches one or more digits in a row.
  2. Step 2: Apply match to string "Room 101"

    The digits "101" are matched as a group, so match[0] returns "101".
  3. Final Answer:

    101 -> Option A
  4. Quick Check:

    Digits matched = 101 [OK]
Quick Trick: \d+ grabs all digits in a row, not just one [OK]
Common Mistakes:
  • Thinking it matches only one digit
  • Confusing match[0] with match object
  • Expecting the word part instead of digits

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes