aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activesupport/lib/active_support/core_ext/string/strip.rb8
-rw-r--r--activesupport/test/core_ext/string_ext_test.rb9
2 files changed, 14 insertions, 3 deletions
diff --git a/activesupport/lib/active_support/core_ext/string/strip.rb b/activesupport/lib/active_support/core_ext/string/strip.rb
index 84d279adbc..086c610976 100644
--- a/activesupport/lib/active_support/core_ext/string/strip.rb
+++ b/activesupport/lib/active_support/core_ext/string/strip.rb
@@ -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 \ No newline at end of file
+end
diff --git a/activesupport/test/core_ext/string_ext_test.rb b/activesupport/test/core_ext/string_ext_test.rb
index d64706ee10..8be65c99f2 100644
--- a/activesupport/test/core_ext/string_ext_test.rb
+++ b/activesupport/test/core_ext/string_ext_test.rb
@@ -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)