Skip to content

Instantly share code, notes, and snippets.

@rewinfrey
Last active August 29, 2015 14:02
Show Gist options
  • Save rewinfrey/297fa49abe8d8942b7de to your computer and use it in GitHub Desktop.
Save rewinfrey/297fa49abe8d8942b7de to your computer and use it in GitHub Desktop.
Simple example for removing leading whitespace with Ruby's heredoc operator
# This is not as sophisticated as ActiveSupport's `strip_heredoc`, but is the simplest solution for
# removing leading whitespace.
# For more nuanced whitespace removal, take a look at http://apidock.com/rails/String/strip_heredoc
puts "With leading whitespace:"
puts <<-TEST
This is a test.
To remove leading spaces.
From Ruby's heredoc operator.
TEST
puts "\nWithout leading whitespace:"
puts <<-TEST.gsub(/^ */, "")
This is a test.
To remove leading spaces.
From Ruby's heredoc operator.
TEST
# >> With leading whitespace:
# >> This is a test.
# >> To remove leading spaces.
# >> From Ruby's heredoc operator.
# >>
# >> Without leading whitespace:
# >> This is a test.
# >> To remove leading spaces.
# >> From Ruby's heredoc operator.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment