let String#strip_heredoc handle blank lines even if they are not indented

This commit is contained in:
Xavier Noria 2010-08-30 10:24:02 +02:00
parent 58d0e2c23c
commit b422cda2eb
2 changed files with 14 additions and 3 deletions

View File

@ -1,3 +1,5 @@
require 'active_support/core_ext/object/try'
class String
# Strips indentation in heredocs.
#
@ -18,7 +20,7 @@ class String
# Technically, it looks for the least indented line in the whole string, and removes
# that amount of leading whitespace.
def strip_heredoc
indent = chomp.scan(/^\s*/).min.size
gsub(/^\s{#{indent}}/, '')
indent = scan(/^[ \t]*(?=\S)/).min.try(:size) || 0
gsub(/^[ \t]{#{indent}}/, '')
end
end
end

View File

@ -33,6 +33,15 @@ class StringInflectionsTest < Test::Unit::TestCase
EOS
end
def test_strip_heredoc_on_a_regular_indented_heredoc_with_blank_lines
assert_equal "foo\n bar\n\nbaz\n", <<-EOS.strip_heredoc
foo
bar
baz
EOS
end
def test_pluralize
SingularToPlural.each do |singular, plural|
assert_equal(plural, singular.pluralize)