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 = "abc123XYZ"
matches = text.scan(/[A-Z]+/)
puts matches.join(",")
Aabc,123,XYZ
BXYZ
CABC
D123
Step-by-Step Solution
Solution:
  1. Step 1: Understand the regex /[A-Z]+/ and scan method

    The regex matches one or more uppercase letters. The scan method finds all matches in the string.
  2. Step 2: Apply regex to the string "abc123XYZ"

    Only "XYZ" matches the uppercase pattern. So matches = ["XYZ"]. Joining with comma results in "XYZ".
  3. Final Answer:

    Output is "XYZ" -> Option B
  4. Quick Check:

    Uppercase matches = "XYZ" [OK]
Quick Trick: Use scan with [A-Z]+ to find uppercase groups [OK]
Common Mistakes:
  • Expecting lowercase or digits to match uppercase regex
  • Confusing scan with match (scan returns all matches)
  • Joining array incorrectly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes