There is no triple-quote multi-line string syntax in Ruby
Tags: ruby
Published: 2021-06-01 20:32:59

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](https://makandracards.com/makandra/1675-using-heredoc-for-prettier-ruby-code). Another convention I've seen in a few codebases are these triple-quote strings.

```ruby
Book.where("""
  where publication_year >= 2000
    and page_count > 200
    and page_count < 500
    and author_id = #{author.id}
""")
```

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

{% twitter 1398059592555405312 %}

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.

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

  This is a multi-line string.
=> nil
```

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

Let's look at another completely different example.

```ruby
> puts "a" + "b" + "c"
abc
=> nil
> puts "a" "b" "c"
abc
=> nil
```

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](https://stackoverflow.com/a/23811744/535590)).

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

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

  This is a multi-line string.
=> nil
```

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.

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

  This is a multi-line string.
=> nil
```

This omits the `+` operators, but has identical behavior.

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

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

  This is a multi-line string.
=> nil
```

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](https://stackoverflow.com/a/27980303/535590)).

### 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](https://learnrubythehardway.org/book/ex10.html).

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](https://crafty-builder-6996.ck.page/e169c61186) or following me on [twitter](https://twitter.com/jbrancha)._

---

<em>Cover photo by <a href="https://unsplash.com/@sevhoein?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Severin Höin</a> on <a href="https://unsplash.com/s/photos/lantern?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Unsplash</a></em>