Bird
0
0

What will be the output of this Ruby code?

medium📝 Predict Output Q13 of 15
Ruby - String Operations
What will be the output of this Ruby code?
text = <<~GREETING
  Hello,
    Welcome to Ruby!
GREETING
puts text
AHello, Welcome to Ruby!
B Hello, Welcome to Ruby!
CHello, Welcome to Ruby!
D!ybuR ot emocleW ,olleH
Step-by-Step Solution
Solution:
  1. Step 1: Understand <<~ operator

    The <<~ syntax removes leading spaces common to all lines, aligning text left.
  2. Step 2: Analyze the given text

    Original lines have 2 spaces before 'Hello,' and 4 before 'Welcome'. The common indent is 2 spaces, so those are removed.
  3. Step 3: Resulting output

    After removing 2 spaces, 'Hello,' starts at column 0, 'Welcome...' starts with 2 spaces.
  4. Final Answer:

    Hello, Welcome to Ruby! -> Option A
  5. Quick Check:

    <<~ removes common indent [OK]
Quick Trick: <<~ removes common leading spaces from all lines [OK]
Common Mistakes:
  • Ignoring <<~ and expecting original indentation
  • Confusing \n with actual new lines
  • Assuming all spaces are removed

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes