public Method

Array.slice!(...)

array.slice!(index)          obj or nil
array.slice!(start, length)  sub_array or nil
array.slice!(range)          sub_array or nil

Deletes the element(s) given by an index (optionally with a length) or by a range. Returns the deleted object, subarray, or nil if the index is out of range. Equivalent to:

def slice!(*args)
  result = self[*args]
  self[*args] = nil
  result
end

a = [ "a", "b", "c" ]
a.slice!(1)     #=> "b"
a               #=> ["a", "c"]
a.slice!(-1)    #=> "c"
a               #=> ["a"]
a.slice!(100)   #=> nil
a               #=> ["a"]

Source Code

/*
*  call-seq:
*     array.slice!(index)         -> obj or nil
*     array.slice!(start, length) -> sub_array or nil
*     array.slice!(range)         -> sub_array or nil 
*  
*  Deletes the element(s) given by an index (optionally with a length)
*  or by a range. Returns the deleted object, subarray, or
*  <code>nil</code> if the index is out of range. Equivalent to:
*     
*     def slice!(*args)
*       result = self[*args]
*       self[*args] = nil
*       result
*     end
*     
*     a = [ "a", "b", "c" ]
*     a.slice!(1)     #=> "b"
*     a               #=> ["a", "c"]
*     a.slice!(-1)    #=> "c"
*     a               #=> ["a"]
*     a.slice!(100)   #=> nil
*     a               #=> ["a"]
*/

static VALUE
rb_ary_slice_bang(argc, argv, ary)
   int argc;
   VALUE *argv;
   VALUE ary;
{
   VALUE arg1, arg2;
   long pos, len;

   if (rb_scan_args(argc, argv, "11", &arg1, &arg2) == 2) {
       pos = NUM2LONG(arg1);
       len = NUM2LONG(arg2);
     delete_pos_len:
       if (pos < 0) {
           pos = RARRAY(ary)->len + pos;
       }
       arg2 = rb_ary_subseq(ary, pos, len);
       rb_ary_splice(ary, pos, len, Qnil);    /* Qnil/rb_ary_new2(0) */
       return arg2;
   }

   if (!FIXNUM_P(arg1) && rb_range_beg_len(arg1, &pos, &len, RARRAY(ary)->len, 1)) {
       goto delete_pos_len;
   }

   return rb_ary_delete_at(ary, NUM2LONG(arg1));
}
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.