Slice or Value of Empty Array

J

Jim Freeze

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
 
J

Jim Freeze

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

Hmm, vaguely. Do you have the reference?
 

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
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top