Array#slice oddity...

M

Matthew Berg

It appears that if you use slice or slice! with a length argument, it
returns an array even if the index is out of range:

irb(main):001:0> a = []
=> []
irb(main):002:0> b = a.slice(0)
=> nil
irb(main):003:0> b = a.slice(0,1)
=> []

Here's the output of ruby -v:

ruby 1.8.0 (2003-08-04) [i686-linux]
 
J

Joel VanderWerf

Matthew said:
It appears that if you use slice or slice! with a length argument, it
returns an array even if the index is out of range:

irb(main):001:0> a = []
=> []
irb(main):002:0> b = a.slice(0)
=> nil
irb(main):003:0> b = a.slice(0,1)
=> []

Shouldn't a[0] and a[0,1][0] return the same value?
 
M

Matthew Berg

Matthew said:
It appears that if you use slice or slice! with a length argument, it
returns an array even if the index is out of range:

irb(main):001:0> a = []
=> []
irb(main):002:0> b = a.slice(0)
=> nil
irb(main):003:0> b = a.slice(0,1)
=> []

Shouldn't a[0] and a[0,1][0] return the same value?

That they do...

irb(main):002:0> a[0]
=> nil
irb(main):003:0> a[0,1][0]
=> nil

Another thing I noticed is that this behaviour is only exhibited if the
start index is 0:

irb(main):004:0> a[0,1]
=> []
irb(main):005:0> a[1,2]
=> nil

The same thing happens with ranges:

irb(main):006:0> a[0..1]
=> []
irb(main):007:0> a[1..2]
=> nil
 
H

Hal Fulton

Matthew said:
Another thing I noticed is that this behaviour is only exhibited if the
start index is 0:

irb(main):004:0> a[0,1]
=> []
irb(main):005:0> a[1,2]
=> nil

The same thing happens with ranges:

irb(main):006:0> a[0..1]
=> []
irb(main):007:0> a[1..2]
=> nil

I think it depends on how far off the end of the
array you go.

x = [1,2,3]
x[3,3] # []
x[4,4] # nil

There's a kind of logic to it, but it requires long and
hard thought (for me, anyway). Search the archives.

Hal
 
M

Matthew Berg

Matthew said:
Another thing I noticed is that this behaviour is only exhibited if the
start index is 0:

irb(main):004:0> a[0,1]
=> []
irb(main):005:0> a[1,2]
=> nil

The same thing happens with ranges:

irb(main):006:0> a[0..1]
=> []
irb(main):007:0> a[1..2]
=> nil

I think it depends on how far off the end of the
array you go.

x = [1,2,3]
x[3,3] # []
x[4,4] # nil

Just to clarify, "how far off the end" in this case seems to be
determined solely by the start value, not the length:

x[3,1000000000] # []
x[4,1] # nil

So I guess it gives you an empty array if you request a start index one
past the end of the array, but only if you didn't specify a length.
There's a kind of logic to it, but it requires long and
hard thought (for me, anyway). Search the archives.

I'll take a look to see if I can find anything on it. One way or
another I can work around it even if it seems a bit counterintuitive.
:)
 
H

Hal Fulton

Matthew said:
I'll take a look to see if I can find anything on it. One way or
another I can work around it even if it seems a bit counterintuitive.
:)

It has something to do with imagining a caret or cursor
in between the elements. When the cursor is outside the
array but at least adjacent to it, that's one case. When
it's just totally outside the bounds, that's the other
case.

You might try searching the archives for "right out" --
a reference to the Holy Hand Grenade of Antioch. ;)

Hal
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top