提交 4dfdc7eb 编写于 作者: J Jordan Thomas 提交者: Rafael França

Frozen truncate (#36109)

* Add test asserting truncate returns unfrozen string

* Ensure strings returned from truncate are not frozen

This fixes an issue where strings too short to be truncated were
returned unfrozen, where as long-enough strings were returned
frozen. Now retuned strings will not be frozen whether or not
the string returned was shortened.

* Update changelog w/ new truncate behavior description

[Jordan Thomas + Rafael Mendonça França]
上级 e281641e
* `truncate` would return the original string if it was too short to be truncated
and a frozen string if it were long enough to be truncated. Now truncate will
consistently return an unfrozen string regardless. This behavior is consistent
with `gsub` and `strip`.
Before:
'foobar'.truncate(5).frozen?
=> true
'foobar'.truncate(6).frozen?
=> false
After:
'foobar'.truncate(5).frozen?
=> false
'foobar'.truncate(6).frozen?
=> false
*Jordan Thomas*
Please check [6-0-stable](https://github.com/rails/rails/blob/6-0-stable/activesupport/CHANGELOG.md) for previous changes.
......@@ -75,7 +75,7 @@ def truncate(truncate_at, options = {})
length_with_room_for_omission
end
"#{self[0, stop]}#{omission}"
+"#{self[0, stop]}#{omission}"
end
# Truncates +text+ to at most <tt>bytesize</tt> bytes in length without
......
......@@ -291,6 +291,11 @@ def test_truncate_with_omission_and_regexp_separator
assert_equal "Hello Big[...]", "Hello Big World!".truncate(15, omission: "[...]", separator: /\s/)
end
def test_truncate_returns_frozen_string
assert_not "Hello World!".truncate(12).frozen?
assert_not "Hello World!!".truncate(12).frozen?
end
def test_truncate_bytes
assert_equal "👍👍👍👍", "👍👍👍👍".truncate_bytes(16)
assert_equal "👍👍👍👍", "👍👍👍👍".truncate_bytes(16, omission: nil)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册