From: Matt Armstrong Date: 2001-11-10T14:05:12+09:00 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