public Method

Integer.downto(p1)

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

Iterates block, passing decreasing values from int down to and including limit.

5.downto(1) { |n| print n, ".. " }
print "  Liftoff!\n"

produces:

5.. 4.. 3.. 2.. 1..   Liftoff!

Source Code

/*
*  call-seq:
*     int.downto(limit) {|i| block }     => int
*  
*  Iterates <em>block</em>, passing decreasing values from <i>int</i>
*  down to and including <i>limit</i>.
*     
*     5.downto(1) { |n| print n, ".. " }
*     print "  Liftoff!\n"
*     
*  <em>produces:</em>
*     
*     5.. 4.. 3.. 2.. 1..   Liftoff!
*/

static VALUE
int_downto(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.