Skip to content

Instantly share code, notes, and snippets.

@wojtekmach
Last active December 20, 2015 10:49
Show Gist options
  • Save wojtekmach/6118846 to your computer and use it in GitHub Desktop.
Save wojtekmach/6118846 to your computer and use it in GitHub Desktop.
Removing whitespace from indented heredoc
require 'minitest/autorun'
class String
def trim
file, line_num = caller.first.split(":")
line = File.read(file).split("\n")[line_num.to_i - 1]
whitespace_num = line[/^[ ]+/].size + 2
self.gsub(/^ {#{whitespace_num}}/, '')
end
end
describe "String#trim" do
it "removes leading whitespace from indented heredoc" do
trimmed = <<-end.trim
level 1
level 2
end
trimmed.must_equal "level 1\n level 2\n"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment