If you have you already know that ruby doesn t have a nice out of the box solution for this problem.
Ruby string interpolation multiline.
Foo this is the first line nthis is the second line n1 1 is 2 n note the absence of a leading newline character.
This is used to embed snippets of code like sql or html.
So why do people prefer string interpolation.
As you can see there are a lot of ways to handle newlines and multiline strings in ruby.
A style guide that reflects real world usage gets used while a style guide that holds to an ideal that has been rejected by the people it is supposed to help risks not getting used at.
Javascript doesn t support any kind of advanced string handling as of ecmascript 5 version 6 supports multi line strings and interpolation so to make it readable we have to handle it using the array syntax.
Multiline strings in ruby 2 3 the squiggly heredoc.
Concatenation looks like this.
Have you ever had to write a nicely indented multiline string in ruby.
This ruby style guide recommends best practices so that real world ruby programmers can write code that can be maintained by other real world ruby programmers.
Damir svrtan 3 min read.
Query sql select from food where healthy true sql you use this syntax to define a heredoc in ruby.
And it does not work in single quoted strings.
Heredocs in ruby also supports string interpolation.
At least this outputs without any extra indentation.
A miscellanea of details on software design.
If you type the code above in your editor and syntax highlighting for ruby code is used it should highlight the code in the double quoted string so it gives you a visual clue about the interpolation.
In your own code choose the style that s right for you and be consistent.
My str foo this is the first line.
This is the second line.
A heredoc is a way to define a multiline string while maintaining the original indentation formatting.
1 1 is 1 1.
You ll encounter all of these methods as you work with existing ruby code as each project tends to have its own style.
In this case a b c creates a new string.