Odd Array#[x..y] return values

D

Daniel Berger

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
 
A

Ara.T.Howard

Hi all,

Ruby 1.8.2

Consider:

irb(main):001:0> a = []
=> []
irb(main):002:0> a[0..1]
=> []

start at a legitimate index and continue with every possible index until the
end, not matter where that is
irb(main):003:0> a[-2..-1]
=> nil

start at a illegitimate index and return that there cannot be anything after
this - ergo 'nil'
Why does the first return [] and the second return nil?

or at least that makes sense to me...

cheers.

-a
--
===============================================================================
| 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
===============================================================================
 
D

Daniel Berger

Ara.T.Howard said:
Hi all,

Ruby 1.8.2

Consider:

irb(main):001:0> a = []
=> []
irb(main):002:0> a[0..1]
=> []

start at a legitimate index and continue with every possible index until the
end, not matter where that is
irb(main):003:0> a[-2..-1]
=> nil

start at a illegitimate index and return that there cannot be anything after
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
 
J

Jason Foreman

On Fri, 22 Jul 2005, Daniel Berger wrote:
=20
Hi all,

Ruby 1.8.2

Consider:

irb(main):001:0> a =3D []
=3D> []
irb(main):002:0> a[0..1]
=3D> []
=20
start at a legitimate index and continue with every possible index unt= il the
end, not matter where that is
=20

Then why this?:

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

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

Ara.T.Howard

Hi all,

Ruby 1.8.2

Consider:

irb(main):001:0> a = []
=> []
irb(main):002:0> a[0..1]
=> []

start at a legitimate index and continue with every possible index until the
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.

yes it does.

-a
--
===============================================================================
| 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
===============================================================================
 
A

Ara.T.Howard

Ara.T.Howard said:
Hi all,

Ruby 1.8.2

Consider:

irb(main):001:0> a = []
=> []
irb(main):002:0> a[0..1]
=> []

start at a legitimate index and continue with every possible index until the
end, not matter where that is
irb(main):003:0> a[-2..-1]
=> nil

start at a illegitimate index and return that there cannot be anything after
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.

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
--
===============================================================================
| 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
===============================================================================
 
B

Brian Candler

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.

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.
 
D

David A. Black

Hi --

Daniel said:
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/[email protected]

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
 
D

Daniel Berger

David said:
Hi --

Daniel said:
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/[email protected]

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,175
Messages
2,570,947
Members
47,498
Latest member
yelene6679

Latest Threads

Top