Posted over 9 years ago. Visible to the public.

Using heredoc for prettier Ruby code

You can use heredoc to avoid endlessly long lines of code that nobody can read. Heredoc strings preserve linebreaks and can be used like this:

Copy
def long_message puts(<<-EOT) Here goes a very long message... Sincerely, foobear EOT end

<<-EOT will be somewhat of a placeholder: anything you write in the line after you used it will be its value until you write EOT in a single line.

You can use any string to flag your heredocs. To be more verbose you can use something else -- your IDE may even be aware of it, for example RubyMine understands <<-SQL, <<-HTML, <<-XML, <<-JSON, etc and highlights correctly.

Please note:

  • Your string will end with a new-line character (\n).

  • Your string's lines will be padded with its indentation. The string from the example above will be " Here goes a very long message...\n Sincerely,\n foobear\n". Depending on where you use it, this may or may not be a problem. A possible solution is <<~ (see below).

  • You can use regular string interpolation inside heredoc strings:

    Copy
    puts <<-TEXT Dear #{user.name}, ... TEXT
  • Your statement does not have to terminate after the placeholder. Example:

    Copy
    content_tag(:div, <<-TEXT, class: 'example') Dear #{user.name}, ... TEXT

<<~ vs <<-

You can use <<~ (with a tilde) to remove the string's indentation. This will keep any relative indentation in other lines.

Copy
<<~TEXT Hello Universe! -foo TEXT # => "Hello\nUniverse!\n -foo\n" # vs: <<-TEXT Hello Universe! -foo TEXT # => " Hello\n Universe!\n -foo\n"

Note that this was introduced with Ruby 2.3. On older Rubies you may use String#strip_heredoc from ActiveSupport.

<< vs <<- (or <<~)

You may also omit the dash or tilde and just write <<EOT. If you do this, your terminating sequence must be at the very beginning of the line. It's less pretty and there is rarely a reason to use it:

Copy
def foo puts(<<TEXT) Hello! TEXT end end

Multiple heredocs in one line

If you have more than one multi-line string that you want to use in one line, you can just put your heredocs strings below each other. Example:

Copy
def long_messages html_escape(<<-ONE) + '<hr />' + html_escape(<<-TWO) Here goes my first very long message. Yeehaw! ONE This is the second message which is still long. It is long indeed. TWO end

Once an application no longer requires constant development, it needs periodic maintenance for stable and secure operation. makandra offers monthly maintenance contracts that let you focus on your business while we make sure the lights stay on.

Owner of this card:

Avatar
Arne Hartherz
Last edit:
about 1 year ago
by Arne Hartherz
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Arne Hartherz to makandra dev
This website uses short-lived cookies to improve usability.
Accept or learn more