Forum: Ruby Array#insert with negative index

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Fbb4d027695dfdf76bf448b15d7e306a?d=identicon&s=25 matt neuburg (Guest)
on 2009-04-03 21:50
(Received via mailing list)
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?

m.
58479f76374a3ba3c69b9804163f39f4?d=identicon&s=25 Eric Hodel (Guest)
on 2009-04-03 22:48
(Received via mailing list)
On Apr 3, 2009, at 12:49, matt neuburg wrote:

> 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.
Fbb4d027695dfdf76bf448b15d7e306a?d=identicon&s=25 matt neuburg (Guest)
on 2009-04-04 00:11
(Received via mailing list)
Eric Hodel <drbrain@segment7.net> wrote:

> > 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].
The indices don't designate spaces between the items; they designate the
items. m.
58479f76374a3ba3c69b9804163f39f4?d=identicon&s=25 Eric Hodel (Guest)
on 2009-04-04 00:27
(Received via mailing list)
On Apr 3, 2009, at 15:09, matt neuburg wrote:
>>> The element with index -1 is 3, the last item. Before is before. I
>> 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
Fbb4d027695dfdf76bf448b15d7e306a?d=identicon&s=25 matt neuburg (Guest)
on 2009-04-04 06:25
(Received via mailing list)
Eric Hodel <drbrain@segment7.net> wrote:

> >>>
> >>> 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?
>
>
> They indicate the insertion point for Array#insert

Cool, I'll buy that, but it is certainly not what the docs say (as
quoted above). I take it no one thinks this is worth filing a bug
over... Thx - m.
D7463bd611f227cfb2ef4da4a978a203?d=identicon&s=25 Christopher Dicely (Guest)
on 2009-04-04 19:44
(Received via mailing list)
On Fri, Apr 3, 2009 at 12:49 PM, matt neuburg <matt@tidbits.com> 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)."

Clearly, a more accurate statement of the existing behavior would be
"Inserts the given values such that the first value (last if the index
given is negative) will have the given index."
B2215101abb1261b9bee5cdda89e7316?d=identicon&s=25 Ian Hobson (Guest)
on 2009-04-04 21:59
(Received via mailing list)
Christopher Dicely wrote:
> "Inserts the given values such that the first value (last if the index
> given is negative) will have the given index."
>
>
This is wrong. (and therefore confusing).

I suspect that what you were trying to say was "The value is inserted so
it becomes the element at the index given". (which is accurate unless an
error occurs).

irb(main):006:0> a = [1,2,3]
=> [1, 2, 3]
irb(main):007:0> a[2]
=> 3
irb(main):008:0> a[-2]
=> 2
irb(main):009:0> a.insert(-2,"x")
=> [1, 2, "x", 3]
irb(main):010:0> [1,2,3].insert(2,"y")
=> [1, 2, "y", 3]
irb(main):011:0>

For the manual..... (Version 1.8.6)

"
insert(pos, val)  will insert object val such that it becomes the
element at position pos, pushing any later elements right one place.

Another way of thinking of this, is that pos is the gap between elements
where the new element will go, with the usual positive and negative
indexing.
For non-negative indexes, 0 is before the first element and you count up
to the right. For negative indexes, -1 is after last element, and you
count down to the left.

Note = positive indexed may extend the array with nil values. Indexes
that are too negative give an "Index error - index out of array" error.
                     "
Regards

Ian
This topic is locked and can not be replied to.