public Method

Array.index(p1)

array.index(obj)     int or nil

Returns the index of the first object in self such that is == to obj. Returns nil if no match is found.

a = [ "a", "b", "c" ]
a.index("b")   #=> 1
a.index("z")   #=> nil

Source Code

/*
*  call-seq:
*     array.index(obj)   ->  int or nil
*  
*  Returns the index of the first object in <i>self</i> such that is 
*  <code>==</code> to <i>obj</i>. Returns <code>nil</code> if
*  no match is found.
*     
*     a = [ "a", "b", "c" ]
*     a.index("b")   #=> 1
*     a.index("z")   #=> nil
*/

static VALUE
rb_ary_index(ary, val)
   VALUE ary;
   VALUE val;
{
   long i;

   for (i=0; i<RARRAY(ary)->len; i++) {
       if (rb_equal(RARRAY(ary)->ptr[i], val))
           return LONG2NUM(i);
   }
   return Qnil;
}
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.