liveBook · Manning

Delimiter

As explained in The Well-Grounded Rubyist, Fourth Edition

A delimiter is a character or string marking the boundary of a literal or heredoc body in Ruby source code.

A delimiter marks where a Ruby literal begins and ends. In percent-style strings, the opening delimiter must match its closing counterpart: paired braces may use left/right forms, while other nonalphanumeric delimiters appear twice. section 8.1.1

Choosing a delimiter

%q-A string- %Q/Another string/ %[Yet another string] With paired delimiters, Ruby tracks nested braces. Thus %Q{I can put {} in here unescaped.} works, while an unmatched brace of the delimiter type must be escaped. section 8.1.1

Heredocs use an identifier as the delimiter: <<EOM starts the body, and a later EOM ends it. The closing identifier can be flush left, or, with <<-EOM, indented; <<~EOM additionally removes common indentation. section 8.1.1

Delimiters also affect how literal content is written:

  • %q and <<-'EOM' produce non-interpolating string forms, preserving interpolation text literally. section 8.1.1
  • A heredoc can appear as an argument, array element, or expression, so its delimiter marks a value within larger Ruby code. section 8.1.1
%q-A string-
%Q/Another string/
%[Yet another string]

Compare several valid percent-string delimiter styles. section 8.1.1

How The Well-Grounded Rubyist, Fourth Edition covers it

How deep it goes
Explains matching, escaping, indentation, interpolation, and expression placement without descending into parser internals.
Examples
Uses percent strings, nested braces, SQL heredocs, indented terminators, arrays, and method arguments.
What it assumes
Assumes familiarity with Ruby strings, quotation marks, escaping, and interpolation.
What you can do after
Apply Ruby delimiter forms to write readable strings and multiline values with controlled interpolation and indentation.
How it connects
Connects string literals, percent notation, interpolation, escaping, and heredocs.

Also explained in

Compare how 11 books explain delimiter →

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.