liveBook · Manning

Squiggly heredoc

As explained in The Well-Grounded Rubyist, Fourth Edition

A squiggly heredoc is a Ruby heredoc form that removes leading indentation from its content.

A squiggly heredoc, written <<~, creates a multiline string while removing leading whitespace from each content line. It is especially useful inside indented method definitions, where ordinary indentation would otherwise become part of the string. section 8.1.1

Indented method output

class RubyWelcomeWagon def squiggly_message <<~EOM Welcome to the world of Ruby! We're happy you're here! EOM end end RubyWelcomeWagon.new.squiggly_message # => "Welcome to the world of Ruby!\nWe're happy you're here!\n"

Compare the related heredoc forms:

  • <<~ permits readable source indentation and removes leading whitespace from the message. section 8.1.1

How The Well-Grounded Rubyist, Fourth Edition covers it

How deep it goes
Defines syntax, demonstrates output, and contrasts content stripping with delimiter indentation.
Examples
Uses a Ruby class method returning a welcome message to show preserved versus stripped indentation.
What it assumes
Assumes familiarity with Ruby strings and heredoc delimiters while contrasting ordinary and hyphenated heredocs.
What you can do after
Apply it via Ruby syntax to keep multiline string source aligned without emitting that indentation.
How it connects
Connects heredocs with multiline strings, method definitions, and string interpolation.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Well-Grounded Rubyist, Fourth Edition. The section links point to the parts of the book it draws on.