public Method

Array.each_index

array.each_index {|index| block }    array

Same as Array#each, but passes the index of the element instead of the element itself.

a = [ "a", "b", "c" ]
a.each_index {|x| print x, " -- " }

produces:

0 -- 1 -- 2 --

Source Code

/*
*  call-seq:
*     array.each_index {|index| block }  ->  array
*  
*  Same as <code>Array#each</code>, but passes the index of the element
*  instead of the element itself.
*     
*     a = [ "a", "b", "c" ]
*     a.each_index {|x| print x, " -- " }
*     
*  produces:
*     
*     0 -- 1 -- 2 --
*/

static VALUE
rb_ary_each_index(ary)
   VALUE ary;
{
   long i;

   for (i=0; i<RARRAY(ary)->len; i++) {
       rb_yield(LONG2NUM(i));
   }
   return ary;
}
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.