From: Mark Hahn Date: 2001-11-11T02:11:16+09:00 Subject: [ruby-talk:24835] Re: Strange behaviour of Array#[] > I now understand the "mental picture" that defines the behavior, but not the "why." I agree. The defense of this behaviour appears to consist of implementation details rather than any logic as to why. I place my vote on "fixing" this behaviour. -----Original Message----- From: matt@lickey.com [mailto:matt@lickey.com]On Behalf Of Matt Armstrong Sent: Friday, November 09, 2001 9:05 PM To: ruby-talk ML Subject: [ruby-talk:24822] Re: Strange behaviour of Array#[] matz@ruby-lang.org (Yukihiro Matsumoto) writes: > Hi, > > In message "[ruby-talk:24773] Re: Strange behaviour of Array#[]" > on 01/11/10, "Lyle Johnson" writes: > > |> I believe it's for consistency with Strings: > | > |This is still pretty squirrelly to me, but hey, it's not my language ;) > > For these methods, each index points at the edge between elements. > > Suppose we have array = [0, 1, 2, 3], > > -4 -3 -2 -1 > +---+---+---+---+ > | 0 | 1 | 2 | 3 | > +---+---+---+---+ > 0 1 2 3 4 > > when the index is not pointing existing edge, array[idx, n] returns > nil (in this case, idx < -4 or 4 < idx). > > Index 4 points the last edge of the array, so it does return a > subarray, which is an empty array since there's no element after the > last edge. Does this make some programming tasks easier? I now understand the "mental picture" that defines the behavior, but not the "why." :-) -- matt