Project

General

Profile

Feature #17056 ยป 17056.patch

Array#rindex change proposal - datibbaw (Tjerk Meesters), 11/20/2026 10:12 AM

View differences:

array.c
*
* Returns +nil+ if the block never returns a truthy value.
*
* When +offset+ is non-negative, it specifies the maximum starting position in the
* array to end the search:
* When +offset+ is non-negative, begins the search backward at position +offset+;
* the returned index is relative to the beginning of +self+:
*
* a = [:foo, 'bar', 2, 'bar']
* a.rindex('bar', offset: 0) # => 3
* a.rindex(2, offset: 1) # => 2
* a.rindex(2, offset: 3) # => nil
* a.rindex('bar', offset: 3) # => 3
* a.rindex('bar', offset: 2) # => 1
* a.rindex('bar', offset: 0) # => nil
* a.rindex('bar', offset: 10) # => 3
*
* With negative integer argument +offset+,
* selects the search position by counting backward from the end of +self+:
*
* a = [:foo, 'bar', 2, 'bar']
* a.rindex('bar', -1) # => 3
* a.rindex('bar', -2) # => 1
* a.rindex('bar', -3) # => 1
* a.rindex('bar', -4) # => nil
* a.rindex('bar', offset: -1) # => 3
* a.rindex('bar', offset: -2) # => 1
* a.rindex('bar', offset: -3) # => 1
* a.rindex('bar', offset: -4) # => nil
*
* When neither an argument nor a block is given, returns a new Enumerator.
*
......
rb_ary_rindex(int argc, VALUE *argv, VALUE ary)
{
VALUE val, opts = Qnil;
rb_len_t i = RARRAY_LEN(ary) - 1, end_pos = 0;
rb_len_t i = RARRAY_LEN(ary) - 1;
int nargc = rb_scan_args(argc, argv, "01:", &val, &opts);
if (nargc == 0) {
......
VALUE offset_arg = offset_option(opts);
if (!UNDEF_P(offset_arg)) {
rb_len_t offset = NUM2LEN(offset_arg);
if (offset < 0) {
if ((i += offset + 1) < 0) return Qnil;
}
else {
if (offset >= RARRAY_LEN(ary)) return Qnil;
end_pos = offset;
}
if (offset < 0 && (offset += RARRAY_LEN(ary)) < 0) return Qnil;
if (offset < i) i = offset;
}
}
for (; i >= end_pos; i--) {
for (; i >= 0; i--) {
VALUE e = RARRAY_AREF(ary, i);
if (nargc == 0 ? RTEST(rb_yield(e)) : rb_equal(e, val)) {
return LEN2NUM(i);
test/ruby/test_array.rb
assert_nil(a.rindex('ca'))
assert_nil(a.rindex([1,2]))
assert_equal(3, a.rindex(99, offset: 0))
assert_equal(3, a.rindex(99, offset: 2))
assert_nil(a.rindex(99, offset: 0))
assert_equal(1, a.rindex(99, offset: 2))
assert_equal(3, a.rindex(99, offset: 3))
assert_equal(1, a.rindex(99, offset: -3))
assert_nil(a.rindex(/a/, offset: 1))
assert_nil(a.rindex(/a/, offset: -4))
assert_nil(a.index(99, offset: a.size))
assert_nil(a.index(99, offset: -a.size - 1))
assert_equal(3, a.rindex(99, offset: a.size))
assert_nil(a.rindex(99, offset: -a.size - 1))
assert_equal(0, a.rindex('cat', offset: -a.size))
assert_nil(@cls[].rindex(99, offset: 0))
assert_equal(3, a.rindex(offset: 0).each {|e| e == 99})
assert_equal(3, a.rindex(offset: 2).each {|e| e == 99})
assert_nil(a.rindex(offset: 0).each {|e| e == 99})
assert_equal(1, a.rindex(offset: 2).each {|e| e == 99})
assert_equal(1, a.rindex(offset: -3).each {|e| e == 99})
assert_equal(3, assert_warning(/given block not used/) {a.rindex(99) {|x| x == [1,2,3] }})
assert_equal(3, assert_warning(/given block not used/) {a.rindex(99, offset: 2) {|x| x == [1,2,3] }})
assert_equal(1, assert_warning(/given block not used/) {a.rindex(99, offset: 2) {|x| x == [1,2,3] }})
bug15951 = "[Bug #15951]"
o2 = Object.new
    (1-1/1)