rindex(p1)
public
Returns the index of the last object in self
to obj. If a block is given instead of an
argument, returns index of first object for which block is true,
starting from the last object. Returns nil
if no match is found. See also Array#index.
If neither block nor argument is given, an enumerator is returned instead.
a = [ "a", "b", "b", "b", "c" ]
a.rindex("b")
a.rindex("z")
a.rindex{|x|x=="b"}
Show source
static VALUE
rb_ary_rindex(int argc, VALUE *argv, VALUE ary)
{
VALUE val;
long i = RARRAY_LEN(ary);
if (argc == 0) {
RETURN_ENUMERATOR(ary, 0, 0);
while (i--) {
if (RTEST(rb_yield(RARRAY_PTR(ary)[i])))
return LONG2NUM(i);
if (i > RARRAY_LEN(ary)) {
i = RARRAY_LEN(ary);
}
}
return Qnil;
}
rb_scan_args(argc, argv, "1", &val);
if (rb_block_given_p())
rb_warn("given block not used");
while (i--) {
if (rb_equal(RARRAY_PTR(ary)[i], val))
return LONG2NUM(i);
if (i > RARRAY_LEN(ary)) {
i = RARRAY_LEN(ary);
}
}
return Qnil;
}