wfxr
11/21/2013 - 3:47 PM

Ruby Multi-line String without Newlines —AND— Ruby Multi-line String without Concatenation

Ruby Multi-line String without Newlines —AND— Ruby Multi-line String without Concatenation

##
# 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