How to Create Multiline Strings in Ruby?

Different ways of creating multiline strings in Ruby

You can create multiline strings in Ruby in the following ways:

Using Multiline String Directly

You could simply use the multiline string directly like so:

str = "Your multiline
    text goes in here";

puts str

# output:
# "Your multiline
#     text goes in here"

As you can see in the example above, the space characters are preserved. Therefore, you need to make sure you remove those if you don't want them to be a part of the string:

str = "Your multiline
text goes in here";

puts str

# output:
# "Your multiline
# text goes in here"

Or, alternatively, you could create the entire string in one line with newline characters (where you want to add a line break):

str = "Your multiline\ntext goes in here";

puts str

# output:
# "Your multiline
# text goes in here"

To make this a bit more readable, you can create multiple chunks of your string and concatenate them using the concatenation operator (+):

str = "Your multiline\n" +
      "text goes in here";

puts str

# output:
# "Your multiline
# text goes in here"

String continuation can also be specified using the backslash character (\) like so:

str = "Your multiline\n" \
      "text goes in here";

puts str

# output:
# "Your multiline
# text goes in here"

You may also use it directly inside the string like so:

str = "You may also put \
a string on multiple lines \
like this";

puts str

# output:
# "You may also put a string on multiple lines like this"

Using the Heredoc Syntax

In Ruby, the heredoc syntax starts with << and is followed by a delimiting identifier (which can be any word or character — by convention written in all caps). This syntax allows you to mark the start and end of a multiline string. In the following example for instance, the delimiting identifier is the word "HEREDOC":

foo = <<HEREDOC
Your multiline
text goes in here
HEREDOC

puts foo

# output:
# "Your multiline
# text goes in here"

When you use this syntax, please note that the whitespace characters are preserved:

foo = <<HEREDOC
    Your multiline
    text goes in here
HEREDOC

puts foo

# output:
# "    Your multiline
#     text goes in here"

If you wish to ignore indentation, then you can use the "squiggly" heredoc syntax instead (which is specified by appending ~ after <<):

foo = <<~HEREDOC
    Your multiline
    text goes in here
HEREDOC

puts foo

# output:
# "Your multiline
# text goes in here"

This also allows you to add indentation before the ending delimiter:

    foo = <<~HEREDOC
    Your multiline
    text goes in here
    HEREDOC

puts foo

# output:
# "Your multiline
# text goes in here"

If you only want to allow indentation before the ending delimiter and not the string itself, then you can use - after <<:

    foo = <<-HEREDOC
    Your multiline
    text goes in here
    HEREDOC

puts foo

# output:
# "    Your multiline
#      text goes in here"

Using Percent Sign Notation (%)

Multiline strings can be created using the % (percent literal syntax):

puts %{Your multiline
text goes in here
}

# output:
# "Your multiline
# text goes in here"

The percent sign indicates that the next character is a literal delimiter (for which you can use any non-alphanumeric character you want). For example, the following would all yield the same output:

puts %(Your multiline
text goes in here
)

puts %~Your multiline
text goes in here
~

puts %|Your multiline
text goes in here
|

# ...

Please note that %() or %{} are more commonly used.

Similar to the examples above, you may also use %Q (which behaves like a double-quote string) and %q (which behaves like a single-quote string — i.e. it does not allow interpolation or character escaping). For example, you can compare the output of the following:

foo = "foo"

puts %q(Your multiline
text goes in here #{foo}
)

# output:
# "Your multiline
# text goes in here #{foo}"
foo = "bar"

puts %Q(Your multiline
text goes in here #{foo}
)

# output:
# "Your multiline
# text goes in here foo"

Hope you found this post useful. It was published . Please show your love and support by sharing this post.