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
irb(main):006:0> a = [1,2,3]
=> [1, 2, 3]
=> [1, 2, “x”, 3]
=> [1, 2, “y”, 3]
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
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.