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

**URL:** <https://rubytalk.org/t/unable-to-go-left-of-an-array-but-yes-right-of-an-array/57425>\
**Category:** ruby-talk\
**Created:** [16 February 2010 00:56 UTC](https://rubytalk.org/t/unable-to-go-left-of-an-array-but-yes-right-of-an-array/57425 "2010-02-16T00:56:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Roger\_Pack4](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@Roger\_Pack4](https://rubytalk.org/u/Roger_Pack4)\
**Post date:** [16 February 2010 00:56 UTC](https://rubytalk.org/t/unable-to-go-left-of-an-array-but-yes-right-of-an-array/57425/1 "2010-02-16T00:56:54Z")

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

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![botp1](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@botp1](https://rubytalk.org/u/botp1)\
**Post date:** [16 February 2010 02:12 UTC](https://rubytalk.org/t/unable-to-go-left-of-an-array-but-yes-right-of-an-array/57425/2 "2010-02-16T02:12:16Z")

</div>

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 8:56 AM, Roger Pack \<rogerpack2005@gmail.com\> wrote:
> 
> > why not
> > 
> > > > [1,2,3][-5..-1]
> > 
> > =\> [1,2,3]

---

<div class="post-metadata">

**Author:** ![Colin\_Bartlett1](https://avatars.discourse-cdn.com/v4/letter/c/dbc845/32.png) [@Colin\_Bartlett1](https://rubytalk.org/u/Colin_Bartlett1)\
**Post date:** [16 February 2010 16:08 UTC](https://rubytalk.org/t/unable-to-go-left-of-an-array-but-yes-right-of-an-array/57425/3 "2010-02-16T16:08:47Z")

</div>

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 ),  
&nbsp;&nbsp;&nbsp;&nbsp;instead of f.floor for the start of the slice.  
&nbsp;&nbsp;&nbsp;&nbsp;Assuming allowing floats as indexes of arrays is reasonable  
&nbsp;&nbsp;&nbsp;&nbsp;(and I'm agnostic on the point for now), I'm very unsure  
&nbsp;&nbsp;&nbsp;&nbsp;whether I'd prefer using to\_int or floor to convert  
&nbsp;&nbsp;&nbsp;&nbsp;a non-integer index to an integer.  
&nbsp;&nbsp;&nbsp;&nbsp;I can see arguments for and against either option.

> **···**
>
> On Tue, Feb 16, 2010 at 12:56 AM, Roger Pack \<rogerpack2005@gmail.com\> 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]
