cycle(...)
public
Calls block for each element of enum repeatedly
n times or forever if none or nil is given. If a non-positive
number is given or the collection is empty, does nothing. Returns nil if
the loop has finished without getting interrupted.
Enumerable#cycle saves elements in an
internal array so changes to enum after the first pass have no effect.
a = ["a", "b", "c"]
a.cycle {|x| puts x }
a.cycle(2) {|x| puts x }
Show source
/*
* call-seq:
* enum.cycle {|obj| block }
* enum.cycle(n) {|obj| block }
*
* Calls <i>block</i> for each element of <i>enum</i> repeatedly _n_
* times or forever if none or nil is given. If a non-positive
* number is given or the collection is empty, does nothing. Returns
* nil if the loop has finished without getting interrupted.
*
* Enumerable#cycle saves elements in an internal array so changes
* to <i>enum</i> after the first pass have no effect.
*
* a = ["a", "b", "c"]
* a.cycle {|x| puts x }
* a.cycle(2) {|x| puts x }
*
*/
static VALUE
enum_cycle(argc, argv, obj)
int argc;
VALUE *argv;
VALUE obj;
{
VALUE ary;
VALUE nv = Qnil;
long n, i, len;
rb_scan_args(argc, argv, "01", &nv);
RETURN_ENUMERATOR(obj, argc, argv);
if (NIL_P(nv)) {
n = -1;
}
else {
n = NUM2LONG(nv);
if (n <= 0) return Qnil;
}
ary = rb_ary_new();
RBASIC(ary)->klass = 0;
rb_block_call(obj, id_each, 0, 0, cycle_i, ary);
len = RARRAY(ary)->len;
if (len == 0) return Qnil;
while (n < 0 || 0 < --n) {
for (i=0; i<len; i++) {
rb_yield(RARRAY(ary)->ptr[i]);
}
}
return Qnil; /* not reached */
}