Feature #16141
closedChange in the array[x,y] notation.
Description
It not a bug, but it's an oddity of the language.
array_four = %w[a b c d e f g]
p array_four[1,3]
p array_four[-1,3]
p array_four[3,3]
p array_four[-3,2]
p array_four[-3, -2]
["b", "c", "d"]
["g"]
["d", "e", "f"]
["e", "f"]
nil
p array_four[-3, -2] do not follow the principal of the least surprise!
the logical output would be ["d", "e"]
The way i see this
p array_four[-3,2] # take 3 step from the end and take 2 elements incrementally
and
p array_four[-3,-2] # take 3 step from the end and take 2 elements decrementally
The change wouldn't affect the retro-compatibility of the language.
Updated by matz (Yukihiro Matsumoto) over 4 years ago
- Status changed from Open to Closed
The y
in ary[x, y]
means the length of resulting subarray, not an index. There's no subarray of negative length.
Matz.
Updated by sawa (Tsuyoshi Sawada) over 4 years ago
This is exactly what I have proposed in #15950. I believe this feature should gain more popularity.
The notion of length is not compatible with negative numbers, but once you extend that notion to a vector, it is natural to have negative quantity.