# Slice or Value of Empty Array

**URL:** <https://rubytalk.org/t/slice-or-value-of-empty-array/10316>\
**Category:** ruby-talk\
**Created:** [25 February 2004 18:34 UTC](https://rubytalk.org/t/slice-or-value-of-empty-array/10316 "2004-02-25T18:34:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jim\_Freeze2](https://avatars.discourse-cdn.com/v4/letter/j/e480ec/32.png) [@Jim\_Freeze2](https://rubytalk.org/u/Jim_Freeze2)\
**Post date:** [25 February 2004 18:34 UTC](https://rubytalk.org/t/slice-or-value-of-empty-array/10316/1 "2004-02-25T18:34:03Z")

</div>

Hi

I am puzzling about the following:

ruby -v -e 'p [][0…0]; p [][0…-1]; p [][-1…-1]'  
ruby 1.8.0 (2003-08-04) [sparc-solaris2.8]  
[]  
[]  
nil

If ary[n…n] returns a slice, then why does a[0…0] give  
me an empty array and a[-1…-1] give me nil?

> **···**
>
> –  
> Jim Freeze
> 
> If you live to the age of a hundred you have it made because very few  
> people die past the age of a hundred.  
> – George Burns

---

<div class="post-metadata">

**Author:** ![Austin\_Ziegler2](https://avatars.discourse-cdn.com/v4/letter/a/ad7895/32.png) [@Austin\_Ziegler2](https://rubytalk.org/u/Austin_Ziegler2)\
**Post date:** [25 February 2004 18:56 UTC](https://rubytalk.org/t/slice-or-value-of-empty-array/10316/2 "2004-02-25T18:56:57Z")

</div>

> Hi  
> I am puzzling about the following:  
> (reformatted)

p [0..0] # =\>   
p [0..-1] # =\>   
p [-1..-1] # =\> nil

> If ary[n..n] returns a slice, then why does a[0..0] give me an  
> empty array and a[-1..-1] give me nil?

Matz explained this some time ago. I think it has something to do  
with indexes sitting _between_ elements in Ruby arrays, and the last  
element being precached as empty.

I’m not sure if that makes any sense…

-austin

> **···**
>
> ## On Thu, 26 Feb 2004 03:34:03 +0900, Jim Freeze wrote:
> 
> austin ziegler \* austin@halostatue.ca \* Toronto, ON, Canada  
> software designer \* pragmatic programmer \* 2004.02.25  
> \* 13.53.09

---

<div class="post-metadata">

**Author:** ![Jim\_Freeze2](https://avatars.discourse-cdn.com/v4/letter/j/e480ec/32.png) [@Jim\_Freeze2](https://rubytalk.org/u/Jim_Freeze2)\
**Post date:** [25 February 2004 19:21 UTC](https://rubytalk.org/t/slice-or-value-of-empty-array/10316/3 "2004-02-25T19:21:20Z")

</div>

Hmm, vaguely. Do you have the reference?

> **···**
>
> On Thursday, 26 February 2004 at 3:56:57 +0900, Austin Ziegler wrote:
> 
> > On Thu, 26 Feb 2004 03:34:03 +0900, Jim Freeze wrote:
> > 
> > > Hi  
> > > I am puzzling about the following:  
> > > (reformatted)
> > 
> > p [0…0] # =\>   
> > p [0…-1] # =\>   
> > p [-1…-1] # =\> nil
> > 
> > > If ary[n…n] returns a slice, then why does a[0…0] give me an  
> > > empty array and a[-1…-1] give me nil?
> > 
> > Matz explained this some time ago. I think it has something to do  
> > with indexes sitting _between_ elements in Ruby arrays, and the last  
> > element being precached as empty.
> > 
> > I’m not sure if that makes any sense…
> 
> –  
> Jim Freeze
> 
> Philosophy will clip an angel’s wings.  
> – John Keats

---

<div class="post-metadata">

**Author:** ![Austin\_Ziegler2](https://avatars.discourse-cdn.com/v4/letter/a/ad7895/32.png) [@Austin\_Ziegler2](https://rubytalk.org/u/Austin_Ziegler2)\
**Post date:** [18 March 2004 04:37 UTC](https://rubytalk.org/t/slice-or-value-of-empty-array/10316/4 "2004-03-18T04:37:18Z")

</div>

[ruby-talk:24773]

Sorry it took so long for me to get back to you on this. I’m not sure why it  
quite works the way that it does for [-1…-1], but that explains the  
concept in general.

-austin

> **···**
>
> On Thu, 26 Feb 2004 04:21:20 +0900, Jim Freeze wrote:
> 
> > On Thursday, 26 February 2004 at 3:56:57 +0900, Austin Ziegler wrote:
> > 
> > > On Thu, 26 Feb 2004 03:34:03 +0900, Jim Freeze wrote:
> > > 
> > > > I am puzzling about the following:  
> > > > (reformatted)
> 
> > > p [0…0] # =\>   
> > > p [0…-1] # =\>   
> > > p [-1…-1] # =\> nil
> 
> > > > If ary[n…n] returns a slice, then why does a[0…0] give me an empty  
> > > > array and a[-1…-1] give me nil?  
> > > > Matz explained this some time ago. I think it has something to do with  
> > > > indexes sitting _between_ elements in Ruby arrays, and the last element  
> > > > being precached as empty.
> > > 
> > > I’m not sure if that makes any sense…  
> > > Hmm, vaguely. Do you have the reference?
> 
> –  
> austin ziegler \* austin@halostatue.ca \* Toronto, ON, Canada  
> software designer \* pragmatic programmer \* 2004.03.17  
> \* 23.33.28
