Last Updated: February 25, 2016
·
3.003K
· blahed

Heredocs with leading whitespace removed

Handy if you like to keep things indented.

def initialize
    @foo = <<-EOF.gsub /^\s+/, ""
        This will not be indented in the resulting string
    EOF
end

And a few other clever ways to handle it: http://stackoverflow.com/questions/3772864/how-do-i-remove-leading-whitespace-chars-from-ruby-heredoc

2 Responses
Add your response

If you're using rails, just use strip_heredoc: http://apidock.com/rails/v3.2.8/String/strip_heredoc

over 1 year ago ·

Yup, and it handles keeping "real" indentions.

over 1 year ago ·