public Method

Array.rindex(p1)

array.rindex(obj)      int or nil

Returns the index of the last object in array == to obj. Returns nil if no match is found.

a = [ "a", "b", "b", "b", "c" ]
a.rindex("b")   #=> 3
a.rindex("z")   #=> nil

Source Code

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

static VALUE
rb_ary_rindex(ary, val)
   VALUE ary;
   VALUE val;
{
   long i = RARRAY(ary)->len;

   while (i--) {
       if (i > RARRAY(ary)->len) {
           i = RARRAY(ary)->len;
           continue;
       }
       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.