aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorJeremy Daer <jeremydaer@gmail.com>2018-02-17 15:23:17 -0800
committerJeremy Daer <jeremydaer@gmail.com>2018-02-17 23:02:19 -0800
commit8454aeeb2b8e7d1255acbaaec4e0ae3c97d55f49 (patch)
treec101ae2b29fbb8a01a97e4d5714a2e96dff43ee6 /activesupport
parent690ce38cfe531ded6cfb282071e21d803297a6dc (diff)
downloadrails-8454aeeb2b8e7d1255acbaaec4e0ae3c97d55f49.tar.gz
rails-8454aeeb2b8e7d1255acbaaec4e0ae3c97d55f49.tar.bz2
rails-8454aeeb2b8e7d1255acbaaec4e0ae3c97d55f49.zip
`String#strip_heredoc` preserves frozenness
```ruby "foo".freeze.strip_heredoc.frozen? # => true ``` Fixes the case where frozen string literals would inadvertently become unfrozen: ```ruby foo = <<-MSG.strip_heredoc la la la MSG foo.frozen? # => false !?? ```
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/CHANGELOG.md16
-rw-r--r--activesupport/lib/active_support/core_ext/string/strip.rb4
-rw-r--r--activesupport/test/core_ext/string_ext_test.rb4
3 files changed, 23 insertions, 1 deletions
diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md
index 33d2793ffc..a844d1d8d7 100644
--- a/activesupport/CHANGELOG.md
+++ b/activesupport/CHANGELOG.md
@@ -1,5 +1,21 @@
## Rails 6.0.0.alpha (Unreleased) ##
+* `String#strip_heredoc` preserves frozenness.
+
+ "foo".freeze.strip_heredoc.frozen? # => true
+
+ Fixes that frozen string literals would inadvertently become unfrozen:
+
+ # frozen_string_literal: true
+
+ foo = <<-MSG.strip_heredoc
+ la la la
+ MSG
+
+ foo.frozen? # => false !??
+
+ *Jeremy Daer*
+
* Rails 6 requires Ruby 2.4.1 or newer.
*Jeremy Daer*
diff --git a/activesupport/lib/active_support/core_ext/string/strip.rb b/activesupport/lib/active_support/core_ext/string/strip.rb
index cc26274e4a..6f9834bb16 100644
--- a/activesupport/lib/active_support/core_ext/string/strip.rb
+++ b/activesupport/lib/active_support/core_ext/string/strip.rb
@@ -20,6 +20,8 @@ class String
# Technically, it looks for the least indented non-empty line
# in the whole string, and removes that amount of leading whitespace.
def strip_heredoc
- gsub(/^#{scan(/^[ \t]*(?=\S)/).min}/, "".freeze)
+ gsub(/^#{scan(/^[ \t]*(?=\S)/).min}/, "".freeze).tap do |stripped|
+ stripped.freeze if frozen?
+ end
end
end
diff --git a/activesupport/test/core_ext/string_ext_test.rb b/activesupport/test/core_ext/string_ext_test.rb
index b8b6387ee5..b95d2d8307 100644
--- a/activesupport/test/core_ext/string_ext_test.rb
+++ b/activesupport/test/core_ext/string_ext_test.rb
@@ -24,6 +24,10 @@ class StringInflectionsTest < ActiveSupport::TestCase
assert_equal "", "".strip_heredoc
end
+ def test_strip_heredoc_on_a_frozen_string
+ assert "".freeze.strip_heredoc.frozen?
+ end
+
def test_strip_heredoc_on_a_string_with_no_lines
assert_equal "x", "x".strip_heredoc
assert_equal "x", " x".strip_heredoc