Contents

Roo

Array.insert!()

Inserts the passed object into this array and returns the modified array.

array.insert!(index as Number, obj as Object) as Array

Parameters

  • index. Integer Number. The index at which the object is to be inserted at. A negative index counts backwards from the end of the array. Therefore an index of -1 will append the object to the end of the array. If index > array.length then we fill the missing elements with Nothing.
  • obj. Object. The object to insert into the array

Returns

  • Returns this array after modification

Example

[1, 2, 3].insert!(0, "a") # => ["a", 1, 2, 3]
[1, 2, 3].insert!(3, "a") # => [1, 2, 3, "a"]
[1, 2, 3].insert!(-1, "a") # => [1, 2, 3, "a"]
[1, 2, 3].insert!(-2, "a") # => [1, 2, "a", 3]
[1, 2, 3].insert!(-4, "a") # => ["a", 1, 2, 3]
[1, 2, 3].insert!(5, "a") # => [1, 2, 3, Nothing, Nothing, "a"]