Hi all,
Ruby 1.8.2
Consider:
irb(main):001:0> a = []
=> []
irb(main):002:0> a[0..1]
=> []
irb(main):003:0> a[-2..-1]
=> nil
Why does the first return [] and the second return nil?
Regards,
Dan
Hi all,
Ruby 1.8.2
Consider:
irb(main):001:0> a = []
=> []
irb(main):002:0> a[0..1]
=> []
irb(main):003:0> a[-2..-1]
=> nil
Why does the first return [] and the second return nil?
Regards,
Dan
Hi all,
Ruby 1.8.2
Consider:
irb(main):001:0> a =
=>
irb(main):002:0> a[0..1]
=>
start at a legitimate index and continue with every possible index until the
end, not matter where that is
irb(main):003:0> a[-2..-1]
=> nil
start at a illegitimate index and return that there cannot be anything after
this - ergo 'nil'
Why does the first return and the second return nil?
or at least that makes sense to me...
cheers.
-a
Daniel Berger wrote:
Hi all,
Ruby 1.8.2
Consider:
irb(main):001:0> a =
=>
irb(main):002:0> a[0..1]
=>
irb(main):003:0> a[-2..-1]
=> nilWhy does the first return and the second return nil?
Archive reading:
http://groups.google.com/groups?threadm=BAY22-F7pSWerOc0Ne000000288@hotmail.com
daz
Ara.T.Howard wrote:
On Fri, 22 Jul 2005, Daniel Berger wrote:
> Hi all,
>
> Ruby 1.8.2
>
> Consider:
>
> irb(main):001:0> a =
> =>
> irb(main):002:0> a[0..1]
> =>start at a legitimate index and continue with every possible index until the
end, not matter where that is> irb(main):003:0> a[-2..-1]
> => nilstart at a illegitimate index and return that there cannot be anything after
this - ergo 'nil'> Why does the first return and the second return nil?
or at least that makes sense to me...
cheers.
-a
Please explain how one is "legitimate" and the other is not. In both
cases, the indices do not exist.
Regards,
Dan
Then why this?:
irb(main):001:0> [0]
=> nil
irb(main):002:0> [0..1]
=>
irb(main):003:0> [1..1]
=> nil
0 is not a legitimate index on an empty array. Seems like a bug to me.
On 7/21/05, Ara.T.Howard <Ara.T.Howard@noaa.gov> wrote:
On Fri, 22 Jul 2005, Daniel Berger wrote:
> Hi all,
>
> Ruby 1.8.2
>
> Consider:
>
> irb(main):001:0> a =
> =>
> irb(main):002:0> a[0..1]
> =>start at a legitimate index and continue with every possible index until the
end, not matter where that is
Hi --
Daniel Berger wrote:
Hi all,
Ruby 1.8.2
Consider:
irb(main):001:0> a =
=>
irb(main):002:0> a[0..1]
=>
irb(main):003:0> a[-2..-1]
=> nilWhy does the first return and the second return nil?
Archive reading:
http://groups.google.com/groups?threadm=BAY22-F7pSWerOc0Ne000000288@hotmail.com
I'll perhaps superfluously report the results of my digging, as long
as I've done it
See the ruby-talk threads starting at:
10136
24700
84973
In the first of these, Matz talks about the index pointing at an
(imaginary) edge between elements.
David
On Fri, 22 Jul 2005, daz wrote:
--
David A. Black
dblack@wobblini.net
yes it does.
-a
On Fri, 22 Jul 2005, Jason Foreman wrote:
On 7/21/05, Ara.T.Howard <Ara.T.Howard@noaa.gov> wrote:
On Fri, 22 Jul 2005, Daniel Berger wrote:
Hi all,
Ruby 1.8.2
Consider:
irb(main):001:0> a =
=>
irb(main):002:0> a[0..1]
=>start at a legitimate index and continue with every possible index until the
end, not matter where that isThen why this?:
irb(main):001:0> [0]
=> nil
irb(main):002:0> [0..1]
=>
irb(main):003:0> [1..1]
=> nil0 is not a legitimate index on an empty array. Seems like a bug to me.
email :: ara [dot] t [dot] howard [at] noaa [dot] gov
phone :: 303.497.6469
My religion is very simple. My religion is kindness.
--Tenzin Gyatso
===============================================================================
if you are thinking in terms of 'c' - which i tend to - an offset of zero from
a thing is the same as the thing. but that clearly is not what's going on
here. i think you are right to call that a bug.
-a
On Fri, 22 Jul 2005, Daniel Berger wrote:
Ara.T.Howard wrote:
On Fri, 22 Jul 2005, Daniel Berger wrote:
Hi all,
Ruby 1.8.2
Consider:
irb(main):001:0> a =
=>
irb(main):002:0> a[0..1]
=>start at a legitimate index and continue with every possible index until the
end, not matter where that isirb(main):003:0> a[-2..-1]
=> nilstart at a illegitimate index and return that there cannot be anything after
this - ergo 'nil'Why does the first return and the second return nil?
or at least that makes sense to me...
cheers.
-a
Please explain how one is "legitimate" and the other is not. In both
cases, the indices do not exist.
email :: ara [dot] t [dot] howard [at] noaa [dot] gov
phone :: 303.497.6469
My religion is very simple. My religion is kindness.
--Tenzin Gyatso
===============================================================================
David A. Black wrote:
Hi --
>
> Daniel Berger wrote:
>> Hi all,
>>
>> Ruby 1.8.2
>>
>> Consider:
>>
>> irb(main):001:0> a =
>> =>
>> irb(main):002:0> a[0..1]
>> =>
>> irb(main):003:0> a[-2..-1]
>> => nil
>>
>> Why does the first return and the second return nil?
>>
>
>
> Archive reading:
> http://groups.google.com/groups?threadm=BAY22-F7pSWerOc0Ne000000288@hotmail.comI'll perhaps superfluously report the results of my digging, as long
as I've done itSee the ruby-talk threads starting at:
10136
24700
84973In the first of these, Matz talks about the index pointing at an
(imaginary) edge between elements.David
Thanks David and Daz.
Maybe this should go in the FAQ. ![]()
Regards,
Dan
On Fri, 22 Jul 2005, daz wrote:
I believe that you are intentionally allowed to go 'one past the end' when
taking slices, for convenience/symmetry reasons.
irb(main):001:0> a=["x","y"]
=> ["x", "y"]
irb(main):002:0> a[0]
=> "x"
irb(main):003:0> a[1]
=> "y"
irb(main):004:0> a[2]
=> nil
irb(main):005:0> a[0,1]
=> ["x"]
irb(main):006:0> a[1,1]
=> ["y"]
irb(main):007:0> a[2,1] #<<<< note
=> #<<<<
irb(main):008:0> a[3,1]
=> nil
irb(main):009:0>
It makes sense if you think of:
irb(main):009:0> a[0..-1]
=> ["x", "y"]
irb(main):010:0> a[1..-1]
=> ["y"]
irb(main):011:0> a[2..-1]
=>
as symmetrical to
irb(main):012:0> a[0,2]
=> ["x", "y"]
irb(main):013:0> a[0,1]
=> ["x"]
irb(main):014:0> a[0,0]
=>
That is, a legitimate array slice operation should return an array, and if
the original array is of size N, we can expect a slice of size between 0 and
N. In that case there are N+1 "sensible" starting positions. Any starting
position outside 0..N is considered invalid and returns nil. (Whether that
should actually raise an exception is off-topic ![]()
In the case of a zero-sized array (N=0): then index 0 is "one past the end".
irb(main):015:0> [0,1]
=>
irb(main):016:0> [1,1]
=> nil
A bit woolly, but perhaps someone else can put it better.
Regards,
Brian.
On Fri, Jul 22, 2005 at 01:20:59PM +0900, Ara.T.Howard wrote:
>Please explain how one is "legitimate" and the other is not. In both
>cases, the indices do not exist.if you are thinking in terms of 'c' - which i tend to - an offset of zero
from
a thing is the same as the thing. but that clearly is not what's going on
here. i think you are right to call that a bug.