public Method

Integer.upto(p1)

int.upto(limit) {|i| block }      int

Iterates block, passing in integer values from int up to and including limit.

5.upto(10) { |i| print i, " " }

produces:

5 6 7 8 9 10

Source Code

/*
*  call-seq:
*     int.upto(limit) {|i| block }     => int
*  
*  Iterates <em>block</em>, passing in integer values from <i>int</i>
*  up to and including <i>limit</i>.
*     
*     5.upto(10) { |i| print i, " " }
*     
*  <em>produces:</em>
*     
*     5 6 7 8 9 10
*/

static VALUE
int_upto(from, to)
   VALUE from, to;
{
   if (FIXNUM_P(from) && FIXNUM_P(to)) {
       long i, end;

       end = FIX2LONG(to);
       for (i = FIX2LONG(from); i <= end; i++) {
           rb_yield(LONG2FIX(i));
       }
   }
   else {
       VALUE i = from, c;

       while (!(c = rb_funcall(i, '>', 1, to))) {
           rb_yield(i);
           i = rb_funcall(i, '+', 1, INT2FIX(1));
       }
       if (NIL_P(c)) rb_cmperr(i, to);
   }
   return from;
}
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.