W
Wojciech Piekutowski
Disclaimer: I know what docs say, but I'd prefer a different
behaviour. I want to understand why this design decision was made.
Some code first:
irb(main):001:0> a = [1,2,3,4,5]
=> [1, 2, 3, 4, 5]
irb(main):002:0> a[0,10]
=> [1, 2, 3, 4, 5] # ad 1
irb(main):003:0> a[-10,10]
=> nil # ad 2
1. Get 10 elements starting from the first element until the end of
the array. If array is smaller than 10 just return everything. This
makes sense, because in the range <0, 10) there are 5 elements that
can be returned.
2. Get 10 elements starting from the 10th element from the end, up to
the end. I got nil, but looking from -10 to 0 there are 5 elements, in
other words in the range (-10, 0> there are 5 elements, like in case
1.
I expected I'd get the same result as in 1 (yes, no additional nils,
just the whole array). I think this isn't really complying with the
principle of the least surprise. I'd be grateful if somebody can
explain me a reasoning behind this behaviour.
Greetings,
Wojciech Piekutowski
behaviour. I want to understand why this design decision was made.
Some code first:
irb(main):001:0> a = [1,2,3,4,5]
=> [1, 2, 3, 4, 5]
irb(main):002:0> a[0,10]
=> [1, 2, 3, 4, 5] # ad 1
irb(main):003:0> a[-10,10]
=> nil # ad 2
1. Get 10 elements starting from the first element until the end of
the array. If array is smaller than 10 just return everything. This
makes sense, because in the range <0, 10) there are 5 elements that
can be returned.
2. Get 10 elements starting from the 10th element from the end, up to
the end. I got nil, but looking from -10 to 0 there are 5 elements, in
other words in the range (-10, 0> there are 5 elements, like in case
1.
I expected I'd get the same result as in 1 (yes, no additional nils,
just the whole array). I think this isn't really complying with the
principle of the least surprise. I'd be grateful if somebody can
explain me a reasoning behind this behaviour.
Greetings,
Wojciech Piekutowski