Ask a Question related to Ruby, Design and Development.
-
Matthew Berg #1
Array#slice oddity...
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>
Matthew Berg Guest
-
Date oddity in Acrobat, default page size oddity
Got two things driving me nuts. One, when I go to Save As a file, the file dats come up in the form like this: Saturday, July -2147483642, 41221,... -
Array.slice seems a bit difference from the document
Hi, I have an array, named as allList And every time I slice a part of the allList to another array, named as partList partList =... -
Toolbox oddity
Greetings All. Does anyone know why the toolbox icons and all the tool options along the top are solid black on some machines and not others, i've... -
Slice an array of hashes?
Is it possible to slice a list of hashes? I read in data into the @data array as follows: while ( $i < $numLev ) { $line = <$fh>; ... -
OE oddity
Greetings, I recently let a friend use my computer for a few minutes to send some e-mail and somehow they managed to mess things up somewhat. ... -
Joel VanderWerf #2
Re: Array#slice oddity...
Matthew Berg wrote:
Shouldn't a[0] and a[0,1][0] return the same value?> 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)
> => []
Joel VanderWerf Guest
-
Matthew Berg #3
Re: Array#slice oddity...
On Tue, 2003-11-11 at 16:45, Joel VanderWerf wrote:
That they do...> 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?
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>
Matthew Berg Guest
-
Hal Fulton #4
Re: Array#slice oddity...
Matthew Berg wrote:
I think it depends on how far off the end of the> 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
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
Hal Fulton Guest
-
Matthew Berg #5
Re: Array#slice oddity...
On Tue, 2003-11-11 at 17:09, Hal Fulton wrote:
Just to clarify, "how far off the end" in this case seems to be> 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
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.
I'll take a look to see if I can find anything on it. One way or> There's a kind of logic to it, but it requires long and
> hard thought (for me, anyway). Search the archives.
another I can work around it even if it seems a bit counterintuitive.
:)
--
Matthew Berg <galt@gothpoodle.com>
Matthew Berg Guest
-
Hal Fulton #6
Re: Array#slice oddity...
Matthew Berg wrote:
It has something to do with imagining a caret or cursor>>>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.
> :)
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
Hal Fulton Guest



Reply With Quote

