aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib
diff options
context:
space:
mode:
authorRafael França <rafael@franca.dev>2019-07-28 00:02:33 -0400
committerGitHub <noreply@github.com>2019-07-28 00:02:33 -0400
commit49238eaf8c24645074682cebc8d78a260cacf5dd (patch)
tree24f3db4726ded7f94efaba7f67a05908e67477ab /activesupport/lib
parentac6f3c9299209ea4b2fa7c368ea1ff406735ca93 (diff)
parent0e2de0e3fdc9a1fc763531b74e9fc49666022ff9 (diff)
downloadrails-49238eaf8c24645074682cebc8d78a260cacf5dd.tar.gz
rails-49238eaf8c24645074682cebc8d78a260cacf5dd.tar.bz2
rails-49238eaf8c24645074682cebc8d78a260cacf5dd.zip
Merge pull request #36185 from jonathanhefner/optimize-string-first-and-last
Improve String#first and #last performance
Diffstat (limited to 'activesupport/lib')
-rw-r--r--activesupport/lib/active_support/core_ext/string/access.rb24
1 files changed, 2 insertions, 22 deletions
diff --git a/activesupport/lib/active_support/core_ext/string/access.rb b/activesupport/lib/active_support/core_ext/string/access.rb
index 4f70e7f5fc..eff31e5493 100644
--- a/activesupport/lib/active_support/core_ext/string/access.rb
+++ b/activesupport/lib/active_support/core_ext/string/access.rb
@@ -76,17 +76,7 @@ class String
# 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
- dup
- else
- to(limit - 1)
- end
+ self[0, limit] || (raise ArgumentError, "negative limit")
end
# Returns the last character of the string. If a limit is supplied, returns a substring
@@ -100,16 +90,6 @@ class String
# 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
- dup
- else
- from(-limit)
- end
+ self[[length - limit, 0].max, limit] || (raise ArgumentError, "negative limit")
end
end