Bird
0
0

What will be the output of this Ruby code?

medium📝 Predict Output Q4 of 15
Ruby - String Operations
What will be the output of this Ruby code?
text = <<~MSG
  Line 1
    Line 2
  Line 3
MSG
puts text
ALine 1 Line 2 Line 3
B Line 1 Line 2 Line 3
CLine 1 Line 2 Line 3
DLine 1 Line 2 Line 3
Step-by-Step Solution
Solution:
  1. Step 1: Understand <<~ removes common leading whitespace

    The <<~ syntax removes the smallest common indentation from all lines.
  2. Step 2: Calculate indentation removal

    Line 1 and Line 3 have 2 spaces, Line 2 has 4 spaces. The smallest common indentation is 2 spaces, so 2 spaces are removed from each line.
  3. Final Answer:

    Line 1 Line 2 Line 3 -> Option A
  4. Quick Check:

    <<~ removes common indent = A [OK]
Quick Trick: <<~ removes smallest common indent from all lines [OK]
Common Mistakes:
  • Removing all indentation from every line
  • Confusing <<~ with <<- which preserves indentation
  • Ignoring indentation on middle lines

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes