Fix for String#undent

Without it, String#undent would fail on unindented strings, e.g.:

    "foo".undent

NoMethodError: undefined method `length' for nil:NilClass`

Closes Homebrew/homebrew#28873.

Signed-off-by: Adam Vandenberg <flangy@gmail.com>
This commit is contained in:
Baptiste Fontaine 2014-05-01 01:10:51 +02:00 committed by Adam Vandenberg
parent 348d0eb05c
commit 74e4fdfce2
2 changed files with 9 additions and 1 deletions

View File

@ -1,6 +1,6 @@
class String
def undent
gsub(/^.{#{slice(/^ +/).length}}/, '')
gsub(/^.{#{(slice(/^ +/) || '').length}}/, '')
end
# eg:

View File

@ -10,4 +10,12 @@ class StringTest < Test::Unit::TestCase
EOS
assert undented == "hi\nmy friend over\nthere\n"
end
def test_undent_not_indented
undented = <<-EOS.undent
hi
I'm not indented
EOS
assert undented == "hi\nI'm not indented\n"
end
end