Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add deprecation warning when String#first and String#last receive neg… #33058

Merged

Conversation

gmcgibbon
Copy link
Member

…ative integers

Summary

This adds a deprecation warning when callingString#first and String#last with negative integer arguments. The current behaviour is the following:

"hello".last(-1) #=> "ello"
"hello".first(-1) #=> "hell"
"hello".last(-6) #=> nil
"hello".first(-6) #=> ""

As you can see, it doesn't really line up with Array's behaviour:

["h", "e", "l", "l", "o"].last(-1) #=> ArgumentError (negative array size)
["h", "e", "l", "l", "o"].first(-1) #=> ArgumentError (negative array size)

See #29105 for the discussion.

@@ -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.0."
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use 6.1 here.

@gmcgibbon gmcgibbon force-pushed the string_first_last_negative_deprecation branch from ac5656a to 9d81130 Compare September 28, 2018 17:57
@gmcgibbon gmcgibbon force-pushed the string_first_last_negative_deprecation branch 2 times, most recently from ccc9402 to 824251d Compare September 28, 2018 18:06
…ative integers

[Gannon McGibbon + Eric Turner]
@gmcgibbon gmcgibbon force-pushed the string_first_last_negative_deprecation branch from 824251d to ec9a89c Compare September 28, 2018 18:29
@rafaelfranca rafaelfranca merged commit cf608ee into rails:master Oct 2, 2018
@gmcgibbon gmcgibbon deleted the string_first_last_negative_deprecation branch October 5, 2018 20:29
suketa added a commit to suketa/rails_sandbox that referenced this pull request Jun 15, 2019
Add deprecation warning when String#first and String#last receive negative integers
rails/rails#33058
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants