# Array#insert with negative index

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.

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.

Eric H. [email protected] 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.

Eric H. [email protected] 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.

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

On Fri, Apr 3, 2009 at 12:49 PM, matt neuburg [email protected] 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.”

Christopher D. 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 forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs