insert(*args)
public
Inserts the given values before the element with the given index.
Negative indices count backwards from the end of the array,
where -1 is the last element.
a = %w{ a b c d }
a.insert(2, 99)
a.insert(-2, 1, 2, 3)
Show source
static VALUE
rb_ary_insert(int argc, VALUE *argv, VALUE ary)
{
long pos;
rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS);
rb_ary_modify_check(ary);
if (argc == 1) return ary;
pos = NUM2LONG(argv[0]);
if (pos == -1) {
pos = RARRAY_LEN(ary);
}
if (pos < 0) {
pos++;
}
rb_ary_splice(ary, pos, 0, rb_ary_new4(argc - 1, argv + 1));
return ary;
}