Bird
0
0

What will be the output of this Ruby code?

medium📝 Predict Output Q5 of 15
Ruby - String Operations
What will be the output of this Ruby code?
text = "123-456-789"
result = text.gsub('-', ':')
puts result
A123:456:789
B123:456-789
C123-456-789
D123456789
Step-by-Step Solution
Solution:
  1. Step 1: Understand gsub behavior

    gsub replaces all occurrences of the pattern, here '-' with ':'.
  2. Step 2: Replace all dashes with colons

    All '-' characters become ':', resulting in "123:456:789".
  3. Final Answer:

    123:456:789 -> Option A
  4. Quick Check:

    gsub replaces all matches = A [OK]
Quick Trick: gsub replaces every match, not just the first [OK]
Common Mistakes:
MISTAKES
  • Expecting only first dash replaced
  • Confusing gsub with sub
  • Removing dashes instead of replacing

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes