rindex(*args)
public
Returns the index of the last object in self == to
obj.
If a block is given instead of an argument, returns the index of the first object for which the block returns
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)
{
const VALUE *ptr;
VALUE val;
long i = RARRAY_LEN(ary), len;
if (argc == 0) {
RETURN_ENUMERATOR(ary, 0, 0);
while (i--) {
if (RTEST(rb_yield(RARRAY_AREF(ary, i))))
return LONG2NUM(i);
if (i > (len = RARRAY_LEN(ary))) {
i = len;
}
}
return Qnil;
}
rb_check_arity(argc, 0, 1);
val = argv[0];
if (rb_block_given_p())
rb_warn("given block not used");
ptr = RARRAY_CONST_PTR(ary);
while (i--) {
VALUE e = ptr[i];
switch (rb_equal_opt(e, val)) {
case Qundef:
if (!rb_equal(e, val)) break;
case Qtrue:
return LONG2NUM(i);
case Qfalse:
continue;
}
if (i > (len = RARRAY_LEN(ary))) {
i = len;
}
ptr = RARRAY_CONST_PTR(ary);
}
return Qnil;
}