# Odd Array#\[x..y\] return values

**URL:** <https://rubytalk.org/t/odd-array-x-y-return-values/19672>\
**Category:** ruby-talk\
**Created:** [22 July 2005 01:35 UTC](https://rubytalk.org/t/odd-array-x-y-return-values/19672 "2005-07-22T01:35:56Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daniel\_Berger2](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/daniel_berger2/32/2243_2.png) [@Daniel\_Berger2](https://rubytalk.org/u/Daniel_Berger2)\
**Post date:** [22 July 2005 01:35 UTC](https://rubytalk.org/t/odd-array-x-y-return-values/19672/1 "2005-07-22T01:35:56Z")

</div>

Hi all,

Ruby 1.8.2

Consider:

irb(main):001:0\> a = []  
=\> []  
irb(main):002:0\> a[0..1]  
=\> []  
irb(main):003:0\> a[-2..-1]  
=\> nil

Why does the first return [] and the second return nil?

Regards,

Dan

---

<div class="post-metadata">

**Author:** ![Ara.T.Howard6](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@Ara.T.Howard6](https://rubytalk.org/u/Ara.T.Howard6)\
**Post date:** [22 July 2005 01:50 UTC](https://rubytalk.org/t/odd-array-x-y-return-values/19672/2 "2005-07-22T01:50:56Z")

</div>

> Hi all,
> 
> Ruby 1.8.2
> 
> Consider:
> 
> irb(main):001:0\> a =   
> =\>   
> irb(main):002:0\> a[0..1]  
> =\>

&nbsp;&nbsp;&nbsp;start at a legitimate index and continue with every possible index until the  
&nbsp;&nbsp;&nbsp;end, not matter where that is

> irb(main):003:0\> a[-2..-1]  
> =\> nil

&nbsp;&nbsp;&nbsp;start at a illegitimate index and return that there cannot be anything after  
&nbsp;&nbsp;&nbsp;this - ergo 'nil'

> Why does the first return and the second return nil?

or at least that makes sense to me...

cheers.

-a

> **···**
>
> # On Fri, 22 Jul 2005, Daniel Berger wrote: --
> 
> > email :: ara [dot] t [dot] howard [at] noaa [dot] gov  
> > phone :: 303.497.6469  
> > My religion is very simple. My religion is kindness.  
> > --Tenzin Gyatso
> 
> ===============================================================================

---

<div class="post-metadata">

**Author:** ![daz](https://avatars.discourse-cdn.com/v4/letter/d/87869e/32.png) [@daz](https://rubytalk.org/u/daz)\
**Post date:** [22 July 2005 09:10 UTC](https://rubytalk.org/t/odd-array-x-y-return-values/19672/3 "2005-07-22T09:10:57Z")

</div>

Daniel Berger wrote:

> Hi all,
> 
> Ruby 1.8.2
> 
> Consider:
> 
> irb(main):001:0\> a =   
> =\>   
> irb(main):002:0\> a[0..1]  
> =\>   
> irb(main):003:0\> a[-2..-1]  
> =\> nil
> 
> Why does the first return and the second return nil?

Archive reading:  
[http://groups.google.com/groups?threadm=BAY22-F7pSWerOc0Ne000000288@hotmail.com](http://groups.google.com/groups?threadm=BAY22-F7pSWerOc0Ne000000288@hotmail.com)

daz

---

<div class="post-metadata">

**Author:** ![Daniel\_Berger2](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/daniel_berger2/32/2243_2.png) [@Daniel\_Berger2](https://rubytalk.org/u/Daniel_Berger2)\
**Post date:** [22 July 2005 02:00 UTC](https://rubytalk.org/t/odd-array-x-y-return-values/19672/4 "2005-07-22T02:00:56Z")

</div>

Ara.T.Howard wrote:

> **···**
>
> > On Fri, 22 Jul 2005, Daniel Berger wrote:
> > 
> > \> Hi all,  
> > \>  
> > \> Ruby 1.8.2  
> > \>  
> > \> Consider:  
> > \>  
> > \> irb(main):001:0\> a =   
> > \> =\>   
> > \> irb(main):002:0\> a[0..1]  
> > \> =\>
> > 
> > &nbsp;&nbsp;&nbsp;start at a legitimate index and continue with every possible index until the  
> > &nbsp;&nbsp;&nbsp;end, not matter where that is
> > 
> > \> irb(main):003:0\> a[-2..-1]  
> > \> =\> nil
> > 
> > &nbsp;&nbsp;&nbsp;start at a illegitimate index and return that there cannot be anything after  
> > &nbsp;&nbsp;&nbsp;this - ergo 'nil'
> > 
> > \> Why does the first return and the second return nil?
> > 
> > or at least that makes sense to me...
> > 
> > cheers.
> > 
> > -a
> 
> Please explain how one is "legitimate" and the other is not. In both  
> cases, the indices do not exist.
> 
> Regards,
> 
> Dan

---

<div class="post-metadata">

**Author:** ![Jason\_Foreman](https://avatars.discourse-cdn.com/v4/letter/j/6a8cbe/32.png) [@Jason\_Foreman](https://rubytalk.org/u/Jason_Foreman)\
**Post date:** [22 July 2005 03:40 UTC](https://rubytalk.org/t/odd-array-x-y-return-values/19672/5 "2005-07-22T03:40:14Z")

</div>

Then why this?:

irb(main):001:0\> [0]  
=\> nil  
irb(main):002:0\> [0..1]  
=\>   
irb(main):003:0\> [1..1]  
=\> nil

0 is not a legitimate index on an empty array. Seems like a bug to me.

> **···**
>
> On 7/21/05, Ara.T.Howard \<Ara.T.Howard@noaa.gov\> wrote:
> 
> > On Fri, 22 Jul 2005, Daniel Berger wrote:
> > 
> > \> Hi all,  
> > \>  
> > \> Ruby 1.8.2  
> > \>  
> > \> Consider:  
> > \>  
> > \> irb(main):001:0\> a =   
> > \> =\>   
> > \> irb(main):002:0\> a[0..1]  
> > \> =\>
> > 
> > &nbsp;&nbsp;&nbsp;start at a legitimate index and continue with every possible index until the  
> > &nbsp;&nbsp;&nbsp;end, not matter where that is

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [22 July 2005 10:49 UTC](https://rubytalk.org/t/odd-array-x-y-return-values/19672/6 "2005-07-22T10:49:34Z")

</div>

Hi --

> Daniel Berger wrote:
> 
> > Hi all,
> > 
> > Ruby 1.8.2
> > 
> > Consider:
> > 
> > irb(main):001:0\> a =   
> > =\>   
> > irb(main):002:0\> a[0..1]  
> > =\>   
> > irb(main):003:0\> a[-2..-1]  
> > =\> nil
> > 
> > Why does the first return and the second return nil?
> 
> Archive reading:  
> [http://groups.google.com/groups?threadm=BAY22-F7pSWerOc0Ne000000288@hotmail.com](http://groups.google.com/groups?threadm=BAY22-F7pSWerOc0Ne000000288@hotmail.com)

I'll perhaps superfluously report the results of my digging, as long  
as I've done it 🙂 See the ruby-talk threads starting at:

10136  
24700  
84973

In the first of these, Matz talks about the index pointing at an  
(imaginary) edge between elements.

David

> **···**
>
> On Fri, 22 Jul 2005, daz wrote:
> 
> --  
> David A. Black  
> dblack@wobblini.net

---

<div class="post-metadata">

**Author:** ![Ara.T.Howard6](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@Ara.T.Howard6](https://rubytalk.org/u/Ara.T.Howard6)\
**Post date:** [22 July 2005 04:11 UTC](https://rubytalk.org/t/odd-array-x-y-return-values/19672/7 "2005-07-22T04:11:37Z")

</div>

yes it does.

-a

> **···**
>
> On Fri, 22 Jul 2005, Jason Foreman wrote:
> 
> > On 7/21/05, Ara.T.Howard \<Ara.T.Howard@noaa.gov\> wrote:
> > 
> > > On Fri, 22 Jul 2005, Daniel Berger wrote:
> > > 
> > > > Hi all,
> > > > 
> > > > Ruby 1.8.2
> > > > 
> > > > Consider:
> > > > 
> > > > irb(main):001:0\> a =   
> > > > =\>   
> > > > irb(main):002:0\> a[0..1]  
> > > > =\>
> > > 
> > > &nbsp;&nbsp;&nbsp;start at a legitimate index and continue with every possible index until the  
> > > &nbsp;&nbsp;&nbsp;end, not matter where that is
> > 
> > Then why this?:
> > 
> > irb(main):001:0\> [0]  
> > =\> nil  
> > irb(main):002:0\> [0..1]  
> > =\>   
> > irb(main):003:0\> [1..1]  
> > =\> nil
> > 
> > 0 is not a legitimate index on an empty array. Seems like a bug to me.
> 
> # --
> 
> > email :: ara [dot] t [dot] howard [at] noaa [dot] gov  
> > phone :: 303.497.6469  
> > My religion is very simple. My religion is kindness.  
> > --Tenzin Gyatso
> 
> ===============================================================================

---

<div class="post-metadata">

**Author:** ![Ara.T.Howard6](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@Ara.T.Howard6](https://rubytalk.org/u/Ara.T.Howard6)\
**Post date:** [22 July 2005 04:20 UTC](https://rubytalk.org/t/odd-array-x-y-return-values/19672/8 "2005-07-22T04:20:59Z")

</div>

if you are thinking in terms of 'c' - which i tend to - an offset of zero from  
a thing is the same as the thing. but that clearly is not what's going on  
here. i think you are right to call that a bug.

-a

> **···**
>
> On Fri, 22 Jul 2005, Daniel Berger wrote:
> 
> > Ara.T.Howard wrote:
> > 
> > > On Fri, 22 Jul 2005, Daniel Berger wrote:
> > > 
> > > > Hi all,
> > > > 
> > > > Ruby 1.8.2
> > > > 
> > > > Consider:
> > > > 
> > > > irb(main):001:0\> a =   
> > > > =\>   
> > > > irb(main):002:0\> a[0..1]  
> > > > =\>
> > > 
> > > &nbsp;&nbsp;&nbsp;start at a legitimate index and continue with every possible index until the  
> > > &nbsp;&nbsp;&nbsp;end, not matter where that is
> > > 
> > > > irb(main):003:0\> a[-2..-1]  
> > > > =\> nil
> > > 
> > > &nbsp;&nbsp;&nbsp;start at a illegitimate index and return that there cannot be anything after  
> > > &nbsp;&nbsp;&nbsp;this - ergo 'nil'
> > > 
> > > > Why does the first return and the second return nil?
> > > 
> > > or at least that makes sense to me...
> > > 
> > > cheers.
> > > 
> > > -a
> > 
> > Please explain how one is "legitimate" and the other is not. In both  
> > cases, the indices do not exist.
> 
> # --
> 
> > email :: ara [dot] t [dot] howard [at] noaa [dot] gov  
> > phone :: 303.497.6469  
> > My religion is very simple. My religion is kindness.  
> > --Tenzin Gyatso
> 
> ===============================================================================

---

<div class="post-metadata">

**Author:** ![Daniel\_Berger2](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/daniel_berger2/32/2243_2.png) [@Daniel\_Berger2](https://rubytalk.org/u/Daniel_Berger2)\
**Post date:** [22 July 2005 14:00 UTC](https://rubytalk.org/t/odd-array-x-y-return-values/19672/9 "2005-07-22T14:00:57Z")

</div>

David A. Black wrote:

> Hi --
> 
> \>  
> \> Daniel Berger wrote:  
> \>\> Hi all,  
> \>\>  
> \>\> Ruby 1.8.2  
> \>\>  
> \>\> Consider:  
> \>\>  
> \>\> irb(main):001:0\> a =   
> \>\> =\>   
> \>\> irb(main):002:0\> a[0..1]  
> \>\> =\>   
> \>\> irb(main):003:0\> a[-2..-1]  
> \>\> =\> nil  
> \>\>  
> \>\> Why does the first return and the second return nil?  
> \>\>  
> \>  
> \>  
> \> Archive reading:  
> \> [http://groups.google.com/groups?threadm=BAY22-F7pSWerOc0Ne000000288@hotmail.com](http://groups.google.com/groups?threadm=BAY22-F7pSWerOc0Ne000000288@hotmail.com)
> 
> I'll perhaps superfluously report the results of my digging, as long  
> as I've done it 🙂 See the ruby-talk threads starting at:
> 
> 10136  
> 24700  
> 84973
> 
> In the first of these, Matz talks about the index pointing at an  
> (imaginary) edge between elements.
> 
> David

Thanks David and Daz.

Maybe this should go in the FAQ. 🙂

Regards,

Dan

> **···**
>
> > On Fri, 22 Jul 2005, daz wrote:

---

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)\
**Post date:** [22 July 2005 08:44 UTC](https://rubytalk.org/t/odd-array-x-y-return-values/19672/10 "2005-07-22T08:44:04Z")

</div>

I believe that you are intentionally allowed to go 'one past the end' when  
taking slices, for convenience/symmetry reasons.

irb(main):001:0\> a=["x","y"]  
=\> ["x", "y"]  
irb(main):002:0\> a[0]  
=\> "x"  
irb(main):003:0\> a[1]  
=\> "y"  
irb(main):004:0\> a[2]  
=\> nil  
irb(main):005:0\> a[0,1]  
=\> ["x"]  
irb(main):006:0\> a[1,1]  
=\> ["y"]  
irb(main):007:0\> a[2,1] #\<\<\<\< note  
=\> #\<\<\<\<  
irb(main):008:0\> a[3,1]  
=\> nil  
irb(main):009:0\>

It makes sense if you think of:

irb(main):009:0\> a[0..-1]  
=\> ["x", "y"]  
irb(main):010:0\> a[1..-1]  
=\> ["y"]  
irb(main):011:0\> a[2..-1]  
=\>

as symmetrical to

irb(main):012:0\> a[0,2]  
=\> ["x", "y"]  
irb(main):013:0\> a[0,1]  
=\> ["x"]  
irb(main):014:0\> a[0,0]  
=\>

That is, a legitimate array slice operation should return an array, and if  
the original array is of size N, we can expect a slice of size between 0 and  
N. In that case there are N+1 "sensible" starting positions. Any starting  
position outside 0..N is considered invalid and returns nil. (Whether that  
should actually raise an exception is off-topic 🙂

In the case of a zero-sized array (N=0): then index 0 is "one past the end".

irb(main):015:0\> [0,1]  
=\>   
irb(main):016:0\> [1,1]  
=\> nil

A bit woolly, but perhaps someone else can put it better.

Regards,

Brian.

> **···**
>
> On Fri, Jul 22, 2005 at 01:20:59PM +0900, Ara.T.Howard wrote:
> 
> > \>Please explain how one is "legitimate" and the other is not. In both  
> > \>cases, the indices do not exist.
> > 
> > if you are thinking in terms of 'c' - which i tend to - an offset of zero  
> > from  
> > a thing is the same as the thing. but that clearly is not what's going on  
> > here. i think you are right to call that a bug.
