Last active
December 20, 2015 10:49
-
-
Save wojtekmach/6118846 to your computer and use it in GitHub Desktop.
Removing whitespace from indented heredoc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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