From: Eric Hodel Date: 2009-04-04T07:25:55+09:00 Subject: Re: Array#insert with negative index On Apr 3, 2009, at 15:09, matt neuburg wrote: > Eric Hodel wrote: >> On Apr 3, 2009, at 12:49, matt neuburg wrote: >> >>> p [1,2,3].insert(-1, "howdy") >>> #=> [1, 2, 3, "howdy"] >>> >>> Docs: "Inserts the given values before the element with the given >>> index >>> (which may be negative)." >>> >>> The element with index -1 is 3, the last item. Before is before. I >>> have >>> a feeling, though, that what's meant here is that when the index is >>> negative, before means after (perhaps because we're looking at the >>> array >>> from the other end, as it were). Is that right? >> >> array: [ 1, 2, 3 ] >> index: 0 1 2 3 >> -4 -3 -2 -1 >> >> So -1 is "one before the end", etc. > > No, I don't follow. I don't get what your diagram is a diagram of. In > an array... > > arr = [1,2,3] > > ...the "3" is arr[2]. It is also arr[-1]. There is no arr[3] or > arr[-4]. Yes there is: irb(main):001:0> [1, 2, 3].insert -4, 'x' => ["x", 1, 2, 3] irb(main):002:0> [1, 2, 3].insert 3, 'x' => [1, 2, 3, "x"] > The indices don't designate spaces between the items; they designate > the > items. m. They indicate the insertion point for Array#insert