未验证 提交 cf608ee3 编写于 作者: R Rafael França 提交者: GitHub

Merge pull request #33058 from gmcgibbon/string_first_last_negative_deprecation

Add deprecation warning when String#first and String#last receive neg…
* Deprecate using negative limits in `String#first` and `String#last`.
*Gannon McGibbon*, *Eric Turner*
* Fix bug where `#without` for `ActiveSupport::HashWithIndifferentAccess` would fail
with symbol arguments
......
......@@ -75,6 +75,10 @@ def to(position)
# str.first(0) # => ""
# str.first(6) # => "hello"
def first(limit = 1)
ActiveSupport::Deprecation.warn(
"Calling String#first with a negative integer limit " \
"will raise an ArgumentError in Rails 6.1."
) if limit < 0
if limit == 0
""
elsif limit >= size
......@@ -95,6 +99,10 @@ def first(limit = 1)
# str.last(0) # => ""
# str.last(6) # => "hello"
def last(limit = 1)
ActiveSupport::Deprecation.warn(
"Calling String#last with a negative integer limit " \
"will raise an ArgumentError in Rails 6.1."
) if limit < 0
if limit == 0
""
elsif limit >= size
......
......@@ -469,6 +469,15 @@ class StringAccessTest < ActiveSupport::TestCase
assert_not_same different_string, string
end
test "#first with negative Integer is deprecated" do
string = "hello"
message = "Calling String#first with a negative integer limit " \
"will raise an ArgumentError in Rails 6.1."
assert_deprecated(message) do
string.first(-1)
end
end
test "#last returns the last character" do
assert_equal "o", "hello".last
assert_equal "x", "x".last
......@@ -487,6 +496,15 @@ class StringAccessTest < ActiveSupport::TestCase
assert_not_same different_string, string
end
test "#last with negative Integer is deprecated" do
string = "hello"
message = "Calling String#last with a negative integer limit " \
"will raise an ArgumentError in Rails 6.1."
assert_deprecated(message) do
string.last(-1)
end
end
test "access returns a real string" do
hash = {}
hash["h"] = true
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册