1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
`Array#to` is working for negative position
This commit is contained in:
Kuldeep Aggarwal 2014-05-23 21:32:52 +05:30
parent 2fdddcee6f
commit 0e9401733c
2 changed files with 5 additions and 1 deletions

View file

@ -15,8 +15,10 @@ class Array
# %w( a b c d ).to(2) # => ["a", "b", "c"]
# %w( a b c d ).to(10) # => ["a", "b", "c", "d"]
# %w().to(0) # => []
# %w( a b c d ).to(-2) # => ["a", "b", "c"]
# %w( a b c ).to(-10) # => []
def to(position)
first position + 1
self[0..position]
end
# Equal to <tt>self[1]</tt>.

View file

@ -16,6 +16,8 @@ class ArrayExtAccessTests < ActiveSupport::TestCase
assert_equal %w( a ), %w( a b c d ).to(0)
assert_equal %w( a b c ), %w( a b c d ).to(2)
assert_equal %w( a b c d ), %w( a b c d ).to(10)
assert_equal %w( a b c ), %w( a b c d ).to(-2)
assert_equal %w(), %w( a b c ).to(-10)
end
def test_second_through_tenth