Skip to main content

Array.insert

insertatitem

Group: Calculations

Documentation

Inserts the given item into the array at the given index.

Arguments

  • at: The index at which to insert the item before. If the index is less than 0, the index will be counted back from the end. If the index is equal to the length of the array, the item will be appended to the end of the array.
  • item: The item to insert into the array.

Examples

Insert 'X' into an array at different locations:

     ['a', 'b', 'c'].to_array.insert 1 'X' == ['a', 'X', 'b', 'c'].to_array
['a', 'b', 'c'].to_array.insert -1 'X' == ['a', 'b', 'X', 'c'].to_array
['a', 'b', 'c'].to_array.insert item='X' == ['a', 'b', 'c', 'X'].to_array