Skip to content

Instantly share code, notes, and snippets.

@squarism
Created June 17, 2015 18:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save squarism/1917084c554375d2fbcd to your computer and use it in GitHub Desktop.
Save squarism/1917084c554375d2fbcd to your computer and use it in GitHub Desktop.
Stripping leading whitespace in ruby heredocs
# <<foo doesn't trim leading whitespace. No problem. :cake:
<<EOS
hi
EOS
=> " hi\n"
# I thought the difference between << and <<- was the leading whitespace trimming.
<<-EOS
hi
EOS
=> " hi\n"
# Am I missing something? Almost all of homebrew monkey patches string to "unindent" heredocs.
# Some people use gsub. Some people monkey patch string. That's fine I guess.
# I think I would use the activesupport gem in a bigger project.
# In a smaller project, just use unindent from homebrew.
require 'active_support/core_ext'
test = <<EOS
hi
EOS
=> " hi\n"
test.strip_heredoc
=> "hi\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment