Slice or Value of Empty Array

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

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

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

[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