# Array#slice oddity

**URL:** <https://rubytalk.org/t/array-slice-oddity/8761>\
**Category:** ruby-talk\
**Created:** [11 November 2003 21:39 UTC](https://rubytalk.org/t/array-slice-oddity/8761 "2003-11-11T21:39:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matthew\_Berg](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@Matthew\_Berg](https://rubytalk.org/u/Matthew_Berg)\
**Post date:** [11 November 2003 21:39 UTC](https://rubytalk.org/t/array-slice-oddity/8761/1 "2003-11-11T21:39:33Z")

</div>

It appears that if you use slice or slice! with a length argument, it  
returns an array even if the index is out of range:

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

Here’s the output of ruby -v:

ruby 1.8.0 (2003-08-04) [i686-linux]

> **···**
>
> –  
> Matthew Berg [galt@gothpoodle.com](mailto:galt@gothpoodle.com)

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf1](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@Joel\_VanderWerf1](https://rubytalk.org/u/Joel_VanderWerf1)\
**Post date:** [11 November 2003 21:45 UTC](https://rubytalk.org/t/array-slice-oddity/8761/2 "2003-11-11T21:45:21Z")

</div>

Matthew Berg wrote:

> It appears that if you use slice or slice! with a length argument, it  
> returns an array even if the index is out of range:
> 
> irb(main):001:0\> a =   
> =\>   
> irb(main):002:0\> b = a.slice(0)  
> =\> nil  
> irb(main):003:0\> b = a.slice(0,1)  
> =\>

Shouldn’t a[0] and a[0,1][0] return the same value?

---

<div class="post-metadata">

**Author:** ![Matthew\_Berg](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@Matthew\_Berg](https://rubytalk.org/u/Matthew_Berg)\
**Post date:** [11 November 2003 22:04 UTC](https://rubytalk.org/t/array-slice-oddity/8761/3 "2003-11-11T22:04:54Z")

</div>

That they do…

irb(main):002:0\> a[0]  
=\> nil  
irb(main):003:0\> a[0,1][0]  
=\> nil

Another thing I noticed is that this behaviour is only exhibited if the  
start index is 0:

irb(main):004:0\> a[0,1]  
=\>   
irb(main):005:0\> a[1,2]  
=\> nil

The same thing happens with ranges:

irb(main):006:0\> a[0…1]  
=\>   
irb(main):007:0\> a[1…2]  
=\> nil

> **···**
>
> On Tue, 2003-11-11 at 16:45, Joel VanderWerf wrote:
> 
> > Matthew Berg wrote:
> > 
> > > It appears that if you use slice or slice! with a length argument, it  
> > > returns an array even if the index is out of range:
> > > 
> > > irb(main):001:0\> a =   
> > > =\>   
> > > irb(main):002:0\> b = a.slice(0)  
> > > =\> nil  
> > > irb(main):003:0\> b = a.slice(0,1)  
> > > =\>
> > 
> > Shouldn’t a[0] and a[0,1][0] return the same value?
> 
> –  
> Matthew Berg [galt@gothpoodle.com](mailto:galt@gothpoodle.com)

---

<div class="post-metadata">

**Author:** ![HAL\_9000](https://avatars.discourse-cdn.com/v4/letter/h/90ced4/32.png) [@HAL\_9000](https://rubytalk.org/u/HAL_9000)\
**Post date:** [11 November 2003 22:09 UTC](https://rubytalk.org/t/array-slice-oddity/8761/4 "2003-11-11T22:09:30Z")

</div>

Matthew Berg wrote:

> Another thing I noticed is that this behaviour is only exhibited if the  
> start index is 0:
> 
> irb(main):004:0\> a[0,1]  
> =\>   
> irb(main):005:0\> a[1,2]  
> =\> nil
> 
> The same thing happens with ranges:
> 
> irb(main):006:0\> a[0…1]  
> =\>   
> irb(main):007:0\> a[1…2]  
> =\> nil

I think it depends on how far off the end of the  
array you go.

x = [1,2,3]  
x[3,3] #   
x[4,4] # nil

There’s a kind of logic to it, but it requires long and  
hard thought (for me, anyway). Search the archives.

Hal

---

<div class="post-metadata">

**Author:** ![Matthew\_Berg](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@Matthew\_Berg](https://rubytalk.org/u/Matthew_Berg)\
**Post date:** [11 November 2003 22:47 UTC](https://rubytalk.org/t/array-slice-oddity/8761/5 "2003-11-11T22:47:22Z")

</div>

> Matthew Berg wrote:
> 
> > Another thing I noticed is that this behaviour is only exhibited if the  
> > start index is 0:
> > 
> > irb(main):004:0\> a[0,1]  
> > =\>   
> > irb(main):005:0\> a[1,2]  
> > =\> nil
> > 
> > The same thing happens with ranges:
> > 
> > irb(main):006:0\> a[0..1]  
> > =\>   
> > irb(main):007:0\> a[1..2]  
> > =\> nil
> 
> I think it depends on how far off the end of the  
> array you go.
> 
> x = [1,2,3]  
> x[3,3] #   
> x[4,4] # nil

Just to clarify, “how far off the end” in this case seems to be  
determined solely by the start value, not the length:

```
    x[3,1000000000] # []
    x[4,1] # nil

```

So I guess it gives you an empty array if you request a start index one  
past the end of the array, but only if you didn’t specify a length.

> There’s a kind of logic to it, but it requires long and  
> hard thought (for me, anyway). Search the archives.

I’ll take a look to see if I can find anything on it. One way or  
another I can work around it even if it seems a bit counterintuitive.  
🙂

> **···**
>
> ## On Tue, 2003-11-11 at 17:09, Hal Fulton wrote:
> 
> Matthew Berg [galt@gothpoodle.com](mailto:galt@gothpoodle.com)

---

<div class="post-metadata">

**Author:** ![HAL\_9000](https://avatars.discourse-cdn.com/v4/letter/h/90ced4/32.png) [@HAL\_9000](https://rubytalk.org/u/HAL_9000)\
**Post date:** [11 November 2003 22:51 UTC](https://rubytalk.org/t/array-slice-oddity/8761/6 "2003-11-11T22:51:44Z")

</div>

Matthew Berg wrote:

> > There’s a kind of logic to it, but it requires long and  
> > hard thought (for me, anyway). Search the archives.
> 
> I’ll take a look to see if I can find anything on it. One way or  
> another I can work around it even if it seems a bit counterintuitive.  
> 🙂

It has something to do with imagining a caret or cursor  
in between the elements. When the cursor is outside the  
array but at least adjacent to it, that’s one case. When  
it’s just totally outside the bounds, that’s the other  
case.

You might try searching the archives for “right out” –  
a reference to the Holy Hand Grenade of Antioch. 😉

Hal
