# Subarray using \[1,-1\] returns nil

**URL:** <https://rubytalk.org/t/subarray-using-1-1-returns-nil/40163>\
**Category:** ruby-talk\
**Created:** [24 August 2007 08:42 UTC](https://rubytalk.org/t/subarray-using-1-1-returns-nil/40163 "2007-08-24T08:42:51Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cliveharber](https://avatars.discourse-cdn.com/v4/letter/c/4da419/32.png) [@Cliveharber](https://rubytalk.org/u/Cliveharber)\
**Post date:** [24 August 2007 08:42 UTC](https://rubytalk.org/t/subarray-using-1-1-returns-nil/40163/1 "2007-08-24T08:42:51Z")

</div>

Hi Todd

The operation b = a[1,-1] is a slice operation where the first index is the start point and the second is the length of the array that you are retrieving - this is why you are getting nil for 'b' - the way to do this would be to use an inclusive range like this b = a[1..-1] - this will return the results that you expect.

---- Todd Burch \<promos@burchwoodusa.com\> wrote:

> **···**
>
> > a = %w(a b c d e f g) ;  
> > puts "a.length = #{a.length}" ;
> > 
> > b = a[1,-1] ;  
> > puts "b.length = #{b.length}" ;
> > 
> > Why is array b nil after the subarray? I expect it to be equal to "a"  
> > minus the first element.
> > 
> > Todd  
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
