Skip to content

Instantly share code, notes, and snippets.

@bkemper
Last active June 14, 2017 14:58
Show Gist options
  • Save bkemper/f61c886dd44fc6f367ccc5974097b88d to your computer and use it in GitHub Desktop.
Save bkemper/f61c886dd44fc6f367ccc5974097b88d to your computer and use it in GitHub Desktop.
Dear Ruby, what about a squished block string?

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:

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
@kbladow
Copy link

kbladow commented Nov 3, 2016

Yes, please

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment