Created
November 21, 2013 15:47
-
-
Save awesome/7584024 to your computer and use it in GitHub Desktop.
Ruby Multi-line String without Newlines
—AND—
Ruby Multi-line String without Concatenation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## | |
# by SoAwesomeMan | |
str =<<-EOS.gsub(/^[\s\t]*|[\s\t]*\n/, '') # no space "\s" for new line "\n"; kill tabs too | |
select awesome, awesome, awesome, awesome, awesome, awesome, | |
from rad, rad, rad, rad, rad, rad, rad, rad, rad, rad, rad, | |
where cool cool cool cool cool cool cool cool cool cool cool' | |
EOS | |
# => "select awesome, awesome, awesome, awesome, awesome, awesome,from rad, rad, rad, rad, rad, rad, rad, rad, rad, rad, rad,where cool cool cool cool cool cool cool cool cool cool cool'" | |
str =<<-EOS.gsub(/^[\s\t]*/, '').gsub(/[\s\t]*\n/, ' ').strip # yes space "\s" for new line "\n"; kill tabs too | |
select awesome, awesome, awesome, awesome, awesome, awesome, | |
from rad, rad, rad, rad, rad, rad, rad, rad, rad, rad, rad, | |
where cool cool cool cool cool cool cool cool cool cool cool' | |
EOS | |
# => "select awesome, awesome, awesome, awesome, awesome, awesome, from rad, rad, rad, rad, rad, rad, rad, rad, rad, rad, rad, where cool cool cool cool cool cool cool cool cool cool cool'" | |
## | |
# via http://stackoverflow.com/questions/2337510/ruby-can-i-write-multi-line-string-with-no-concatenation | |
# by nocache => http://stackoverflow.com/users/778675/nocache | |
# "if you don't mind the extra newlines being inserted:" | |
conn.exec 'select attr1, attr2, attr3, attr4, attr5, attr6, attr7 | |
from table1, table2, table3, etc, etc, etc, etc, etc, | |
where etc etc etc etc etc etc etc etc etc etc etc etc etc' | |
# "Alternatively you can use a heredoc:" | |
<<-eos | |
select attr1, attr2, attr3, attr4, attr5, attr6, attr7 | |
from table1, table2, table3, etc, etc, etc, etc, etc, | |
where etc etc etc etc etc etc etc etc etc etc etc etc etc' | |
eos |
if you want to have space instead of new lines with indention possibility:
<<~EOS.gsub(/\n/, '').squeeze
a
b
EOS
FYI-- I found that adding .squeeze also removes double letters unless you pass in a blank space. Thanks for the solution, though! https://apidock.com/ruby/String/squeeze
(update-- also, use squish
instead of the gsub
!)
<<-TEXT.squish.squeeze(' ')
aa
b
TEXT
# => "aa b"
Hey @awesome - I was here trying to create a complex docker command. Hope you and your family are happy and healthy.
One can use <<~
+ split("\n").map(&:strip).join
, eg:
expect("<ul><li></li></ul>").to eq(<<~HTML.split("\n").map(&:strip).join
<ul>
<li>
</li>
</ul>
HTML
)
<<~TOKEN.squish.tr(' ', '')
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwidXNlcm5hbWUiOiJhdHVueTAiLCJlbWFpbCI6ImF0dW55MEBzb2h1LmNvbSI
sImZpcnN0TmFtZSI6IlRlcnJ5IiwibGFzdE5hbWUiOiJNZWRodXJzdCIsImdlbmRlciI6Im1hbGUiLCJpbWFnZSI6Imh0dHBzOi8vcm9ib2h
hc2gub3JnL2hpY3ZlbGRpY3RhLnBuZyIsImlhdCI6MTY5ODQ0MjQyNywiZXhwIjoxNjk4NDQ2MDI3fQ._4x0c9aKr3LIP-GKycd_TGlH_DT3
joV-0OGHvDxVSkA
TOKEN
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can use the tilde character
~
to remove tabs so you can indent your heredoc now.