aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/test
diff options
context:
space:
mode:
authorKasper Timm Hansen <kaspth@gmail.com>2019-07-26 22:00:56 +0200
committerKasper Timm Hansen <kaspth@gmail.com>2019-07-26 22:08:34 +0200
commitefa2299ac3bbf86feacfa337645654f87109e769 (patch)
treea16064fea665505ed59aeb5113a332f6f325c172 /activesupport/test
parenta0e58e687dec52d42ebadfbb72e1eae449b13ba5 (diff)
downloadrails-efa2299ac3bbf86feacfa337645654f87109e769.tar.gz
rails-efa2299ac3bbf86feacfa337645654f87109e769.tar.bz2
rails-efa2299ac3bbf86feacfa337645654f87109e769.zip
Remove tough to grasp -1 + 1 = 0 from String#to
In case a negative position is provided that exceeds the size of the string, we're relying on -1 returned from max to get 0 length by + 1 and let [] with a 0 length returning "" for us. E.g. "hello".to(-7), where -7 + 5 size = -2. That's lower than -1, so we use -1 instead and + 1 would turn it into 0. Instead allow outer bounds access and always return "".
Diffstat (limited to 'activesupport/test')
-rw-r--r--activesupport/test/core_ext/string_ext_test.rb2
1 files changed, 2 insertions, 0 deletions
diff --git a/activesupport/test/core_ext/string_ext_test.rb b/activesupport/test/core_ext/string_ext_test.rb
index c5a000b67a..b1ab9533e7 100644
--- a/activesupport/test/core_ext/string_ext_test.rb
+++ b/activesupport/test/core_ext/string_ext_test.rb
@@ -455,6 +455,8 @@ class StringAccessTest < ActiveSupport::TestCase
test "#to with negative Integer, position is counted from the end" do
assert_equal "hell", "hello".to(-2)
+ assert_equal "h", "hello".to(-5)
+ assert_equal "", "hello".to(-7)
end
test "#from and #to can be combined" do