Bird
0
0

What will be the output of this Ruby code?

medium📝 Predict Output Q4 of 15
Ruby - Regular Expressions
What will be the output of this Ruby code?
text = "abc123xyz"
matches = text.scan(/\d+/)
puts matches.join(",")
A123
Babc,xyz
Cabc123xyz
DError
Step-by-Step Solution
Solution:
  1. Step 1: Understand scan with regex \d+

    The regex \d+ matches one or more digits. scan returns all such matches as an array.
  2. Step 2: Analyze the code output

    text.scan(/\d+/) returns ["123"]. Joining with comma results in "123".
  3. Final Answer:

    123 -> Option A
  4. Quick Check:

    scan with digits returns "123" [OK]
Quick Trick: scan returns array of matches; join converts to string [OK]
Common Mistakes:
  • Expecting letters instead of digits
  • Confusing scan with match
  • Thinking output is original string

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes