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 = "hello 123 world 456"
matches = text.scan(/\d+/)
puts matches.inspect
A["123", "456"]
B["hello", "world"]
C["1", "2", "3", "4", "5", "6"]
D[]
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the regex pattern

    The pattern /\d+/ matches sequences of digits, not individual digits.
  2. Step 2: Apply scan to the string

    It finds "123" and "456" as two separate matches.
  3. Final Answer:

    ["123", "456"] -> Option A
  4. Quick Check:

    scan with /\d+/ returns digit groups = D [OK]
Quick Trick: scan with /\d+/ returns digit groups, not single digits [OK]
Common Mistakes:
  • Expecting individual digits instead of groups
  • Confusing words with digits
  • Assuming empty array if digits present

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes