instance method
Array#rindex
(untyped obj) -> ::Integer?
() { (Elem item) -> ::boolish } -> ::Integer?
() -> ::Enumerator[Elem, ::Integer?]
rindex(object) → integer or nil
rindex {|element| ... } → integer or nil
rindex → new_enumerator
array.c
View on GitHub ↗
static VALUE
rb_ary_rindex(int argc, VALUE *argv, VALUE ary)
{
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");
while (i--) {
VALUE e = RARRAY_AREF(ary, i);
if (rb_equal(e, val)) {
return LONG2NUM(i);
}
if (i > RARRAY_LEN(ary)) {
break;
}
}
return Qnil;
}
Returns the index of the last element for which object == element.
With argument object given, returns the index of the last such element found:
Example
a = [:foo, 'bar', 2, 'bar'] a.rindex('bar') # => 3
Returns nil if no such object found.
With a block given, calls the block with each successive element; returns the index of the last element for which the block returns a truthy value:
Example
a = [:foo, 'bar', 2, 'bar'] a.rindex {|element| element == 'bar' } # => 3
Returns nil if the block never returns a truthy value.
When neither an argument nor a block is given, returns a new Enumerator.
Related: see Methods for Querying.