public Method

Array.delete(p1)

array.delete(obj)             obj or nil 
array.delete(obj) { block }   obj or nil

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")                   #=> "b"
a                               #=> ["a", "c"]
a.delete("z")                   #=> nil
a.delete("z") { "not found" }   #=> "not found"

Source Code

/*
*  call-seq:
*     array.delete(obj)            -> obj or nil 
*     array.delete(obj) { block }  -> obj or nil
*  
*  Deletes items from <i>self</i> that are equal to <i>obj</i>. If
*  the item is not found, returns <code>nil</code>. If the optional
*  code block is given, returns the result of <i>block</i> if the item
*  is not found.
*     
*     a = [ "a", "b", "b", "b", "c" ]
*     a.delete("b")                   #=> "b"
*     a                               #=> ["a", "c"]
*     a.delete("z")                   #=> nil
*     a.delete("z") { "not found" }   #=> "not found"
*/

VALUE
rb_ary_delete(ary, item)
   VALUE ary;
   VALUE item;
{
   long i1, i2;

   for (i1 = i2 = 0; i1 < RARRAY(ary)->len; i1++) {
       VALUE e = RARRAY(ary)->ptr[i1];

       if (rb_equal(e, item)) continue;
       if (i1 != i2) {
           rb_ary_store(ary, i2, e);
       }
       i2++;
   }
   if (RARRAY(ary)->len == i2) {
       if (rb_block_given_p()) {
           return rb_yield(item);
       }
       return Qnil;
   }

   rb_ary_modify(ary);
   if (RARRAY(ary)->len > i2) {
       RARRAY(ary)->len = i2;
       if (i2 * 2 < RARRAY(ary)->aux.capa &&
           RARRAY(ary)->aux.capa > ARY_DEFAULT_SIZE) {
           REALLOC_N(RARRAY(ary)->ptr, VALUE, i2 * 2);
           RARRAY(ary)->aux.capa = i2 * 2;
       }
   }

   return item;
}
Comments

Have your say
Please use Textile formatting (click here for a cheat sheet). Use <code/> and <pre/> for code samples.
Click here to login with OpenID to to post comments.