Is this expected?
[1,2,3][0…55]
=> [1, 2, 3][1,2,3][-5…-1]
=> nil
why not
[1,2,3][-5…-1]
=> [1,2,3]
?
Thanks.
-rp
Is this expected?
[1,2,3][0…55]
=> [1, 2, 3][1,2,3][-5…-1]
=> nil
why not
[1,2,3][-5…-1]
=> [1,2,3]
?
Thanks.
-rp
On Tue, Feb 16, 2010 at 8:56 AM, Roger P. [email protected]
wrote:
why not
[1,2,3][-5…-1]
=> [1,2,3]
i wont answer your question.
but on my case, i’d like both cases to return nil or fail. ![]()
best regards -botp
On Tue, Feb 16, 2010 at 12:56 AM, Roger P. [email protected]
wrote:
Is this expected?
[1,2,3][0…55]
=> [1, 2, 3]
[1,2,3][-5…-1]
=> nilwhy not
[1,2,3][-5…-1]
=> [1,2,3]
My understanding is that what decides whether a slice or nil is returned
is whether the starting point of the potential slice is “in” the
array.
So that is what I’d expect, albeit I think one could reasonably have
different conventions, such as the ones you and botp suggest.
What does seem maybe a bit strange to me are the “??” below:,
a = [0, 1, 2] #=> [0, 1, 2]
a[0, 5] #=> [0, 1, 2]
a[0.75, 5] #=> [0, 1, 2]
a[1, 5] #=> [1, 2]
a[2, 5] #=> [2]
a[3, 5] #=> [] ?? (1)
a[3.75, 5] #=> [] ?? (1)
a[4, 5] #=> nil
a[4.75, 5] #=> nil
a[-1, 5] #=> [2]
a[-2, 5] #=> [1, 2]
a[-3, 5] #=> [0, 1, 2]
a[-4, 5] #=> nil
a[-0.75, 5] #=> [0, 1, 2] ?? (2)
a[-1.75, 5] #=> [2] ?? (2)
a[-2.75, 5] #=> [1, 2] ?? (2)
a[-3.75, 5] #=> [0, 1, 2] ?? (2)
a[-4.75, 5] #=> nil
(1) Are there many people who want this usage, instead of wanting nil?
(2) These seem to use f.truncate === f.to_int === Integer( f ),
instead of f.floor for the start of the slice.
Assuming allowing floats as indexes of arrays is reasonable
(and I’m agnostic on the point for now), I’m very unsure
whether I’d prefer using to_int or floor to convert
a non-integer index to an integer.
I can see arguments for and against either option.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs