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 hidden or 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 |
pboling
commented
Oct 27, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment