public Method

Array.replace(p1)

array.replace(other_array)   array

Replaces the contents of self with the contents of other_array, truncating or expanding if necessary.

a = [ "a", "b", "c", "d", "e" ]
a.replace([ "x", "y", "z" ])   #=> ["x", "y", "z"]
a                              #=> ["x", "y", "z"]

Source Code

/*
*  call-seq:
*     array.replace(other_array)  -> array
*  
*  Replaces the contents of <i>self</i> with the contents of
*  <i>other_array</i>, truncating or expanding if necessary.
*     
*     a = [ "a", "b", "c", "d", "e" ]
*     a.replace([ "x", "y", "z" ])   #=> ["x", "y", "z"]
*     a                              #=> ["x", "y", "z"]
*/

static VALUE
rb_ary_replace(copy, orig)
   VALUE copy, orig;
{
   VALUE shared;

   rb_ary_modify(copy);
   orig = to_ary(orig);
   if (copy == orig) return copy;
   shared = ary_make_shared(orig);
   if (RARRAY(copy)->ptr && !FL_TEST(copy, ELTS_SHARED))
       free(RARRAY(copy)->ptr);
   RARRAY(copy)->ptr = RARRAY(orig)->ptr;
   RARRAY(copy)->len = RARRAY(orig)->len;
   RARRAY(copy)->aux.shared = shared;
   FL_SET(copy, ELTS_SHARED);

   return copy;
}
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.