Skip to content

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.

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.

2 participants