← Back to Ruby Course | Chapter 3: Strings | Lesson 3 of 7

Heredoc

In this page:

  1. Heredoc

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

markup
name = "World"

text = <<~GREETING
  Hello, #{name}!
  This is a heredoc.
  It can span many lines.
GREETING

puts text
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.