If I have an array a:
a[a.length]
returnsnil
. Good.a[a.length, x]
returns[]
. Good.a[a.length+x, y]
returnsnil
. Inconsistent with 2.
While this behavior is documented, it seems odd.
Can anybody explain the reasons behind this design?
If I have an array a:
a[a.length]
returns nil
. Good.a[a.length, x]
returns []
. Good.a[a.length+x, y]
returns nil
. Inconsistent with 2.While this behavior is documented, it seems odd.
Can anybody explain the reasons behind this design?
Consider this
>> a=[0,1,2,3]
=> [0, 1, 2, 3]
>> a[0,10]
=> [0, 1, 2, 3]
>> a[1,10]
=> [1, 2, 3]
>> a[2,10]
=> [2, 3]
>> a[3,10]
=> [3]
>> a[4,10]
=> []
>> a[5,10]
=> nil
So a[4,10]
is the slice between the 3
and the end of the array which is []
Where as a[4]
and a[5,10]
are accessing elements that aren't in the array
It may help to think of the slice points as being between the elements, rather than the elements themselves.