What is the output of this Ruby code using heredoc syntax?
Ruby
str = <<-TEXT
Hello,
World!
TEXT
puts str
AHello,\nWorld!\n
BHello, World!
CHello,\nWorld!
DHello,World!\n
Attempts:
2 left
💡 Hint
Remember that heredoc preserves newlines exactly as written.
✗ Incorrect
The heredoc syntax preserves the line breaks and includes a final newline. So the output includes both lines and ends with a newline.
❓ Predict Output
intermediate
2:00remaining
Indentation behavior with <<- heredoc
What will this Ruby code print?
Ruby
str = <<-DOC
Line one
Line two
DOC
puts str
A Line one\n Line two\n
BLine one\nLine two\n
Cowt eniL eno eniL
D Line one Line two
Attempts:
2 left
💡 Hint
The <<- syntax allows the end marker to be indented but does not remove indentation inside the string.
✗ Incorrect
The indentation inside the heredoc content is preserved exactly, so the two lines start with two spaces each.
❓ Predict Output
advanced
2:00remaining
Difference between <<- and <<~ in heredoc
What is the output of this Ruby code?
Ruby
str = <<~HEREDOC
Hello
World
HEREDOC
puts str
AHello\nWorld\n
B Hello\n World\n
CHello\n World\n
D Hello\n World\n"
Attempts:
2 left
💡 Hint
The <<~ syntax removes the least common leading whitespace from all lines.
✗ Incorrect
The <<~ removes the smallest indentation common to all lines, so the first line's 4 spaces are removed from both lines, preserving relative indentation.
🧠 Conceptual
advanced
2:00remaining
Heredoc with variable interpolation
Given this Ruby code, what will be the output?
Ruby
name = "Alice"
str = <<-TEXT
Hello, #{name}!
Welcome.
TEXT
puts str
The heredoc with <<- uses double quotes by default, so #{name} is replaced with Alice.
🔧 Debug
expert
3:00remaining
Why does this heredoc cause a syntax error?
This Ruby code raises a SyntaxError. What is the cause?
Ruby
str = <<HEREDOC
Hello
World
HEREDOC
puts str
AThe heredoc content must be indented to match the ending marker.
BThe ending HEREDOC marker is indented, but << without a dash requires it to be at the start of the line.
CThe heredoc content must not contain blank lines.
DThe heredoc marker must be quoted to avoid errors.
Attempts:
2 left
💡 Hint
Check the position of the ending marker when using << without a dash.
✗ Incorrect
When using <
Practice
(1/5)
1. What is the main purpose of using heredoc syntax in Ruby?
easy
A. To declare variables with fixed values
B. To create single-line comments
C. To write multiline strings easily without using escape characters
D. To define methods with multiple parameters
Solution
Step 1: Understand what heredoc does
Heredoc syntax allows writing strings that span multiple lines without needing escape characters like \n.
Step 2: Compare options
Declaring variables with fixed values, creating single-line comments, and defining methods with multiple parameters describe unrelated Ruby features. Only writing multiline strings easily without using escape characters correctly describes heredoc's purpose.
Final Answer:
To write multiline strings easily without using escape characters -> Option C
Quick Check:
Heredoc = multiline strings [OK]
Hint: Heredoc is for easy multiline text, not comments or methods [OK]
Common Mistakes:
Confusing heredoc with comments
Thinking heredoc defines methods
Assuming heredoc is for variables only
2. Which of the following is the correct syntax to start a heredoc string named TEXT in Ruby?
easy
A. << TEXT
B. <TEXT
C.
D. TEXT<<
Solution
Step 1: Recall heredoc start syntax
Heredoc starts with << followed immediately by an identifier, e.g., <
Step 2: Check each option
<<TEXT matches the correct syntax. << TEXT with space is invalid. Arrangements like
Final Answer:
<<TEXT -> Option B
Quick Check:
Start heredoc with <<IDENTIFIER [OK]
Hint: Heredoc starts with << followed by the identifier [OK]
Common Mistakes:
Putting identifier before <<
Using incorrect symbols like < or - wrongly
Confusing heredoc start with end
3. What will be the output of this Ruby code?
text = <<~GREETING
Hello,
Welcome to Ruby!
GREETING
puts text
medium
A. Hello,
Welcome to Ruby!
B. Hello,
Welcome to Ruby!
C. Hello,
Welcome to Ruby!
D. !ybuR ot emocleW
,olleH
Solution
Step 1: Understand <<~ operator
The <<~ syntax removes leading spaces common to all lines, aligning text left.
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.
Step 3: Resulting output
After removing 2 spaces, 'Hello,' starts at column 0, 'Welcome...' starts with 2 spaces.
Final Answer:
Hello,
Welcome to Ruby! -> Option A
Quick Check:
<<~ removes common indent [OK]
Hint: <<~ 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
4. Identify the error in this Ruby heredoc code:
message = <
medium
A. The string must be enclosed in quotes
B. The starting identifier should be lowercase
C. The heredoc must use <<~ instead of <<
D. The ending identifier must match exactly and be alone on its line
Solution
Step 1: Check heredoc start and end identifiers
Heredoc must end with the same identifier used to start it, alone on a line with no spaces.
Step 2: Analyze given code
Start identifier is GREETING but end identifier is END, which is incorrect.
Final Answer:
The ending identifier must match exactly and be alone on its line -> Option D
Quick Check:
Start and end identifiers must match [OK]
Hint: End heredoc with the exact same word alone on a line [OK]
Common Mistakes:
Mismatching start and end identifiers
Adding spaces after end identifier
Using lowercase instead of uppercase identifiers
5. You want to create a multiline string with indentation preserved but also want to remove leading spaces common to all lines for neat code. Which heredoc syntax should you use in Ruby?
hard
A. <<~TEXT
B. <<-TEXT
C. <TEXT
D. <<=TEXT
Solution
Step 1: Understand heredoc variants
<<TEXT preserves all spaces; <<-TEXT allows indenting the end marker; <<~TEXT removes common leading spaces from all lines.
Step 2: Match requirement
To keep indentation inside the string but remove common leading spaces for clean code, <<~TEXT is the correct choice.
Final Answer:
<<~TEXT -> Option A
Quick Check:
<<~ trims common indent [OK]
Hint: <<~ removes common indent, use it for clean multiline strings [OK]
Common Mistakes:
Using <<- which only affects end marker indentation
Using << which keeps all spaces
Assuming <<= is valid syntax
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.