Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save awesome/7584024 to your computer and use it in GitHub Desktop.
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
##
# 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
@Startouf
Copy link

You can use the tilde character ~ to remove tabs so you can indent your heredoc now.

  some_code_that_was_indented: <<~EOS,
    code parsed by heredoc
    over several lines
       with maybe some line more indented
    with indentation preserved in the code file
    but removed when the string is compiled
    based on the heredoc lines' smallest indentation
  EOS

@andrejska
Copy link

if you want to have space instead of new lines with indention possibility:

<<~EOS.gsub(/\n/, '').squeeze
  a
                  b
EOS

@scottgratton
Copy link

scottgratton commented Nov 16, 2018

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"

@voodoologic
Copy link

Hey @awesome - I was here trying to create a complex docker command. Hope you and your family are happy and healthy.

@glaucocustodio
Copy link

glaucocustodio commented Jul 24, 2023

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
)

@pboling
Copy link

pboling commented Oct 27, 2023

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