public Method

Integer.times

int.times {|i| block }      int

Iterates block int times, passing in values from zero to int - 1.

5.times do |i|
  print i, " "
end

produces:

0 1 2 3 4

Source Code

/*
*  call-seq:
*     int.times {|i| block }     => int
*  
*  Iterates block <i>int</i> times, passing in values from zero to
*  <i>int</i> - 1.
*     
*     5.times do |i|
*       print i, " "
*     end
*     
*  <em>produces:</em>
*     
*     0 1 2 3 4
*/

static VALUE
int_dotimes(num)
   VALUE num;
{
   if (FIXNUM_P(num)) {
       long i, end;

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

       for (;;) {
           if (!RTEST(rb_funcall(i, '<', 1, num))) break;
           rb_yield(i);
           i = rb_funcall(i, '+', 1, INT2FIX(1));
       }
   }
   return num;
}
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.