From: Josh Cheek Date: 2013-03-05T16:16:45+09:00 Subject: Re: Inconsistent Array#slice behavior? --f46d040890c15ec19704d72841f4 Content-Type: text/plain; charset=ISO-8859-1 On Tue, Mar 5, 2013 at 1:01 AM, Jack R. wrote: > Given x = [], > > x[0, 2] => [] > x[-3,2] => nil > > Documentation says that out-of-range indices produce a nil result, but 0 > s/b out-of-range for an empty array, shouldn't it? > > Hmm, docs I usually use say "Additionally, an empty array is returned when the starting index for an element range is at the end of the array. Returns nil if the index (or starting index) are out of range." ( http://rdoc.info/stdlib/core/Array:slice) In this case, all elements other than 0 are past the end of the array. It might be easier to see if you had an element in there: [:element][-2,1] # => nil [:element][-1,1] # => [:element] [:element][ 0,1] # => [:element] [:element][ 1,1] # => [] [:element][ 2,1] # => nil --f46d040890c15ec19704d72841f4 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable On Tue, Mar 5, 2013 at 1:01 AM, Jack R. <lists@ruby-forum.com> wrote:
Given x =3D [],

x[0, 2] =3D> []
x[-3,2] =3D> nil

Documentation says that out-of-range indices produce a nil result, but 0 s/b out-of-range for an empty array, shouldn't it?


Hmm, docs I usually use say "Additionally, an empty array is return= ed when the starting index for an element range is at the end of the array.= =A0Returns=A0nil=A0if the index (or starting index) are out of range." (= http://rdoc.info/std= lib/core/Array:slice)
=
In this case, all elements other than 0 are past the end o= f the array. It might be easier to see if you had an element in there:
=
[:element][-2,1] =A0# =3D> nil
[:element= ][-1,1] =A0# =3D> [:element]
[:element][ 0,1] =A0# =3D> [:element]
[:element][ 1,1] = =A0# =3D> []
[:element][ 2,1] =A0# =3D> nil

<= /div>

--f46d040890c15ec19704d72841f4--