delete(p1)
public
Deletes items from self that are equal to obj. If the
item is not found, returns nil. If the optional code block is given,
returns the result of block if the item is not found.
a = [ "a", "b", "b", "b", "c" ]
a.delete("b")
a
a.delete("z")
a.delete("z") { "not found" }
Show source
VALUE
rb_ary_delete(VALUE ary, VALUE item)
{
VALUE v = item;
long i1, i2;
for (i1 = i2 = 0; i1 < RARRAY_LEN(ary); i1++) {
VALUE e = RARRAY_PTR(ary)[i1];
if (rb_equal(e, item)) {
v = e;
continue;
}
if (i1 != i2) {
rb_ary_store(ary, i2, e);
}
i2++;
}
if (RARRAY_LEN(ary) == i2) {
if (rb_block_given_p()) {
return rb_yield(item);
}
return Qnil;
}
rb_ary_modify(ary);
if (RARRAY_LEN(ary) > i2) {
ARY_SET_LEN(ary, i2);
if (i2 * 2 < ARY_CAPA(ary) &&
ARY_CAPA(ary) > ARY_DEFAULT_SIZE) {
ary_resize_capa(ary, i2*2);
}
}
return v;
}