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:
parent
348d0eb05c
commit
74e4fdfce2
@ -1,6 +1,6 @@
|
||||
class String
|
||||
def undent
|
||||
gsub(/^.{#{slice(/^ +/).length}}/, '')
|
||||
gsub(/^.{#{(slice(/^ +/) || '').length}}/, '')
|
||||
end
|
||||
|
||||
# eg:
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user