public Method

Array.*(p1)

array * int         an_array
array * str         a_string

Repetition—With a String argument, equivalent to self.join(str). Otherwise, returns a new array built by concatenating the int copies of self.

[ 1, 2, 3 ] * 3    #=> [ 1, 2, 3, 1, 2, 3, 1, 2, 3 ]
[ 1, 2, 3 ] * ","  #=> "1,2,3"

Source Code

/* 
*  call-seq:
*     array * int     ->    an_array
*     array * str     ->    a_string
*
*  Repetition---With a String argument, equivalent to
*  self.join(str). Otherwise, returns a new array
*  built by concatenating the _int_ copies of _self_.
*
*
*     [ 1, 2, 3 ] * 3    #=> [ 1, 2, 3, 1, 2, 3, 1, 2, 3 ]
*     [ 1, 2, 3 ] * ","  #=> "1,2,3"
*
*/

static VALUE
rb_ary_times(ary, times)
   VALUE ary, times;
{
   VALUE ary2, tmp;
   long i, len;

   tmp = rb_check_string_type(times);
   if (!NIL_P(tmp)) {
       return rb_ary_join(ary, tmp);
   }

   len = NUM2LONG(times);
   if (len == 0) return ary_new(rb_obj_class(ary), 0);
   if (len < 0) {
       rb_raise(rb_eArgError, "negative argument");
   }
   if (LONG_MAX/len < RARRAY(ary)->len) {
       rb_raise(rb_eArgError, "argument too big");
   }
   len *= RARRAY(ary)->len;

   ary2 = ary_new(rb_obj_class(ary), len);
   RARRAY(ary2)->len = len;

   for (i=0; i<len; i+=RARRAY(ary)->len) {
       MEMCPY(RARRAY(ary2)->ptr+i, RARRAY(ary)->ptr, VALUE, RARRAY(ary)->len);
   }
   OBJ_INFECT(ary2, ary);

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