[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Array#slice oddity...

Matthew Berg

11/11/2003 9:40:00 PM

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]

--
Matthew Berg <galt@gothpoodle.com>


5 Answers

Joel VanderWerf

11/11/2003 9:45:00 PM

0

Matthew Berg wrote:
> 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?


Matthew Berg

11/11/2003 10:05:00 PM

0

On Tue, 2003-11-11 at 16:45, Joel VanderWerf wrote:
> Matthew Berg wrote:
> > 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

--
Matthew Berg <galt@gothpoodle.com>


Hal E. Fulton

11/11/2003 10:10:00 PM

0

Matthew Berg wrote:
> 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



Matthew Berg

11/11/2003 10:47:00 PM

0

On Tue, 2003-11-11 at 17:09, Hal Fulton wrote:
> Matthew Berg wrote:
> > 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.
:)
--
Matthew Berg <galt@gothpoodle.com>


Hal E. Fulton

11/11/2003 10:52:00 PM

0

Matthew Berg wrote:
>>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.
> :)

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