HEREDOCs can be used to write multiline strings that respect the maximum line length set in your project. However, the output may not look as expected due to the preserved format and indentation. To address this issue, you can use the squish method with a HEREDOC to leave your output clean.

When working on a project, respecting line length is an essential best practice. If you're working with a block of code, you can split your method into multiple smaller methods to keep the lines short. However, when you're working with a literal string that exceeds the maximum number of characters per line set in your project, using the backslash (\) can make the code hard to read, especially if you have SQL queries in your code. This is where HEREDOCs come in handy.

HEREDOCs are a way to write multiline strings while keeping their format and indentation. They even support interpolation, making them a great tool to use for long SQL queries or HTML code. Here's an example:

query = <<-SQL SELECT users.id, users.first_name, users.last_name FROM users WHERE users.age >= 18 SQL # Output: " SELECT users.id, users.first_name, users.last_name\n FROM users\n WHERE users.age >= 18\n"

This example splits a long line into multiple shorter ones, and the linter won't complain about line length. However, when you look at your terminal output or your app's log, you'll notice that this code is displayed with a different format. While all the other output is shown inline, this piece of code preserves all the new lines and spaces (at the beginning of each line).

To solve this problem, you can combine the HEREDOC with some string methods, such as strip. This removes the spaces and new lines at the beginning and end of the string, but not in between lines. Here's an example:

query = <<-SQL.strip SELECT users.id, users.first_name, users.last_name FROM users WHERE users.age >= 18 SQL # Output: "SELECT users.id, users.first_name, users.last_name\n FROM users\n WHERE users.age >= 18"

This example removes the spaces and new lines at the beginning and end of the string, but not in between lines. To remove the spaces and new lines in between lines, you can use the squish method, which removes all whitespace, including new lines and spaces. Here's an example:

query = <<-SQL.squish SELECT users.id, users.first_name, users.last_name FROM users WHERE users.age >= 18 SQL # Output: "SELECT users.id, users.first_name, users.last_name FROM users WHERE users.age >= 18"

Ruby 2.3 introduced the squiggly HEREDOC, which uses a tilde (~) instead of a hyphen (-), like this: <<~HEREDOC. The result is similar to the example that uses the regular HEREDOC followed by squish, except that it leaves a newline character at the end of the string. Adding strip to this example will leave a single-line string.

Supposing you have a query with sub-levels marked by spaces, the squiggly HEREDOC will preserve those spaces, so you will have to use the squish to output the string in a single line without those spaces.

And that's it! You now have a clean multiline string that respects the best practices rules in your project and has a clear and neat output in the terminal, logs, or any other output method you're using.

Have you tried this before? Let us know your thoughts or comments! Reach out to us!