Ruby supports a number of ways to define a block/multiline string.
- Concatenation
- Heredocs
- Join an array of strings
- Quotes
- Shorthand
Here are some explanations:
- Ruby: Multiline strings - here doc or quotes
- A Cheatsheet for Multi-line Strings in Ruby
- Multiline strings in Ruby 2.3 - the squiggly heredoc
With that said, I have a use case that can almost be satisfied by the squiggly heredoc, but it would still need to be squished. I would like an operator that I can split on multiple lines to adhere to line limits, but remains a single line.
My single line message:
MY_MESSAGE = <<~TEXT
This is a really really long message that will exceed my max line length of
80 characters, but splitting this message into multiple lines is ugly. What
would you do?
TEXT
The squiggly heredoc allows me to indent to make this readable, but the result included newlines.
puts MY_MESSAGE
"This is a really really long message that will exceed my max line length of\n80 characters, but splitting this message into multiple lines is ugly. What\nwould you do?"
It would be nice if I could double squiggly to replace each newline with a space to maintain a single line string.
The current workaround:
MY_MESSAGE = <<~TEXT.squish
This is a really really long message that will exceed my max line length of
80 characters, but splitting this message into multiple lines is ugly. What
would you do?
TEXT
Yes, please