rindex(...)
public
Returns the index of the last object in array == to
obj. If a block is given instead of an argument, returns first object for which block is true.
Returns nil if no match is found.
a = [ "a", "b", "b", "b", "c" ]
a.rindex("b")
a.rindex("z")
a.rindex{|x|x=="b"}
Show source
/*
* 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>. If a block is given instead of an
* argument, returns first object for which <em>block</em> is
* true. Returns <code>nil</code> if no match is found.
*
* a = [ "a", "b", "b", "b", "c" ]
* a.rindex("b")
* a.rindex("z")
* a.rindex{|x|x=="b"}
*/
static VALUE
rb_ary_rindex(argc, argv, ary)
int argc;
VALUE *argv;
VALUE ary;
{
VALUE val;
long i = RARRAY(ary)->len;
if (argc == 0) {
RETURN_ENUMERATOR(ary, 0, 0);
while (i--) {
if (RTEST(rb_yield(RARRAY(ary)->ptr[i])))
return LONG2NUM(i);
if (i > RARRAY(ary)->len) {
i = RARRAY(ary)->len;
}
}
return Qnil;
}
rb_scan_args(argc, argv, "01", &val);
while (i--) {
if (rb_equal(RARRAY(ary)->ptr[i], val))
return LONG2NUM(i);
if (i > RARRAY(ary)->len) {
i = RARRAY(ary)->len;
}
}
return Qnil;
}