*(p1)
public
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 ] * ","
Show source
static VALUE
rb_ary_times(VALUE ary, VALUE times)
{
VALUE ary2, tmp, *ptr, *ptr2;
long i, t, len;
tmp = rb_check_string_type(times);
if (!NIL_P(tmp)) {
return rb_ary_join(ary, tmp);
}
len = NUM2LONG(times);
if (len == 0) {
ary2 = ary_new(rb_obj_class(ary), 0);
goto out;
}
if (len < 0) {
rb_raise(rb_eArgError, "negative argument");
}
if (ARY_MAX_SIZE/len < RARRAY_LEN(ary)) {
rb_raise(rb_eArgError, "argument too big");
}
len *= RARRAY_LEN(ary);
ary2 = ary_new(rb_obj_class(ary), len);
ARY_SET_LEN(ary2, len);
ptr = RARRAY_PTR(ary);
ptr2 = RARRAY_PTR(ary2);
t = RARRAY_LEN(ary);
for (i=0; i<len; i+=t) {
MEMCPY(ptr2+i, ptr, VALUE, t);
}
out:
OBJ_INFECT(ary2, ary);
return ary2;
}