# Unable to go "left" of an array, but yes "right" of an array

**URL:** <https://www.ruby-forum.com/t/unable-to-go-left-of-an-array-but-yes-right-of-an-array/183518>\
**Category:** Ruby\
**Created:** [February 16, 2010, 1:52am UTC](https://www.ruby-forum.com/t/unable-to-go-left-of-an-array-but-yes-right-of-an-array/183518 "2010-02-16T01:52:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [February 16, 2010, 1:52am UTC](https://www.ruby-forum.com/t/unable-to-go-left-of-an-array-but-yes-right-of-an-array/183518/1 "2010-02-16T01:52:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [February 16, 2010, 3:16am UTC](https://www.ruby-forum.com/t/unable-to-go-left-of-an-array-but-yes-right-of-an-array/183518/2 "2010-02-16T03:16:07Z")

</div>

On Tue, Feb 16, 2010 at 8:56 AM, Roger P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [February 16, 2010, 5:14pm UTC](https://www.ruby-forum.com/t/unable-to-go-left-of-an-array-but-yes-right-of-an-array/183518/3 "2010-02-16T17:14:53Z")

</div>

On Tue, Feb 16, 2010 at 12:56 AM, Roger P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> 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]

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.
