*(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;
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) {
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);
for (i=0; i<len; i+=RARRAY_LEN(ary)) {
MEMCPY(RARRAY_PTR(ary2)+i, RARRAY_PTR(ary), VALUE, RARRAY_LEN(ary));
}
out:
OBJ_INFECT(ary2, ary);
return ary2;
}