Array#insert with negative index

M

matt neuburg

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.
 
E

Eric Hodel

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.
 
E

Eric Hodel

Eric Hodel said:
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
 
C

Christopher Dicely

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."
 
I

Ian Hobson

Christopher said:
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."
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top