DEV Community

Cover image for There is no triple-quote multi-line string syntax in Ruby
Josh Branchaud
Josh Branchaud

Posted on

There is no triple-quote multi-line string syntax in Ruby

A lot of languages distinguish between single-line and multi-line strings. They have specialized syntax for declaring one or the other.

Not Ruby.

In Ruby, any string can be a multi-line string if you hit the Enter key a few times.

Nevertheless, there are conventions. When I want a multi-line string, I tend to reach for a heredoc. Another convention I've seen in a few codebases are these triple-quote strings.

Book.where("""
  where publication_year >= 2000
    and page_count > 200
    and page_count < 500
    and author_id = #{author.id}
""")
Enter fullscreen mode Exit fullscreen mode

Like me, you probably look at that triple-quote syntax and think, "yeah, I'm pretty sure I've seen that before."

Fun Side Quest: try finding where in the Ruby docs this syntax feature is discussed.

Don't spend too long searching though. As the title of this post suggests, there is no triple-quote multi-line string syntax.

So, what gives? Why does this work and where did it come from?

Let's start with "why does this work?"

First, let's demonstrate that we can do multi-line strings with the "standard" double-quote syntax.

> puts "
  This is a multi-line string.
"

  This is a multi-line string.
=> nil
Enter fullscreen mode Exit fullscreen mode

It prints out the string, newlines and extra spaces included. And as expected, puts returns nil.

Let's look at another completely different example.

> puts "a" + "b" + "c"
abc
=> nil
> puts "a" "b" "c"
abc
=> nil
Enter fullscreen mode Exit fullscreen mode

These two lines do identical things. A feature of the Ruby parser and interpreter—actually not a feature so much as an obtuse implementation detail adopted from C—is that adjacent strings are concatenated together (source).

Let's see how that plays into our multi-line string example.

> puts "" + "
  This is a multi-line string.
" + ""

  This is a multi-line string.
=> nil
Enter fullscreen mode Exit fullscreen mode

This explicitly concatenates empty strings on to either end of our multi-line string with the + operator.

As we just learned, that is equivalent to the following.

> puts "" "
  This is a multi-line string.
" ""

  This is a multi-line string.
=> nil
Enter fullscreen mode Exit fullscreen mode

This omits the + operators, but has identical behavior.

We can take this a step further by removing those superfluous spaces.

> puts """
  This is a multi-line string.
"""

  This is a multi-line string.
=> nil
Enter fullscreen mode Exit fullscreen mode

Again, the same behavior.

So what appears to be some special triple-quote multi-line string syntax is instead a syntactic trick that exploits a Ruby interpreter feature where adjacent strings are concatenated (source).

On to the other question, "where did this come from?"

The best I can tell is that this syntactic convention was introduced to Ruby developers through Learn Ruby The Hard Way.

I hope that was enlightening or at least cleared up some confusion. For my part, I will continue to reach for heredocs when appropriate and do my best to remove this pseudo-syntax from the Ruby codebases I work in.

If you enjoy my writing, consider joining my newsletter or following me on twitter.


Cover photo by Severin Höin on Unsplash

Top comments (0)