提交 8454aeeb 编写于 作者: J Jeremy Daer

`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 !??
```
上级 690ce38c
## 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*
......
......@@ -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
......@@ -24,6 +24,10 @@ def test_strip_heredoc_on_an_empty_string
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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册