Ruby Newbie homepage Ruby Newbie homepage

How to use

Quick guide

Official content
Calls the given block self times with each integer in (0..self-1):
a = []
5.times {|i| a.push(i) } # => 5
a                        # => [0, 1, 2, 3, 4]
With no block given, returns an Enumerator.
 
               static VALUE
int_dotimes(VALUE num)
{
    RETURN_SIZED_ENUMERATOR(num, 0, 0, int_dotimes_size);

    if (FIXNUM_P(num)) {
        long i, end;

        end = FIX2LONG(num);
        for (i=0; i<end; i++) {
            rb_yield_1(LONG2FIX(i));
        }
    }
    else {
        VALUE i = INT2FIX(0);

        for (;;) {
            if (!RTEST(int_le(i, num))) break;
            rb_yield(i);
            i = rb_int_plus(i, INT2FIX(1));
        }
    }
    return num;
}
            

Was this page useful?