Heredoc
In this page:
Heredoc
A heredoc, started with <<~IDENTIFIER and ended with a matching IDENTIFIER on its own line, lets you write a large, readable block of multi-line text. The ~ variant (squiggly heredoc) automatically strips leading indentation, making it easy to keep the heredoc nicely indented with surrounding code. Heredocs support string interpolation just like double-quoted strings.
Note: Prefer <<~ (squiggly heredoc) over the older <<- since it strips common leading whitespace automatically.
Example: Heredoc
name = "World"
text = <<~GREETING
Hello, #{name}!
This is a heredoc.
It can span many lines.
GREETING
puts text
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: