From 675bcd889415548562a23b8d35e6bcb262b62dd3 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Thu, 27 Aug 2026 14:50:00 +0200 Subject: [PATCH] Add Array#rindex MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Array#rindex was missing entirely. It now searches backward from the end: rindex(obj) returns the last index whose element == obj; rindex { |e| … } the last index whose block is truthy (an argument, when given, wins over the block, as MRI does); and with no argument and no block it returns a sized Enumerator. Returns nil when nothing matches. Verified against ruby 4.0.6. Whole-package coverage stays at 100%; the ruby/spec floor rises 15820 -> 15830 (core/array/rindex 0 -> 9). Co-Authored-By: Claude Opus 4.8 --- internal/vm/array_edges_test.go | 22 ++++++++++++++++++++++ internal/vm/builtins.go | 22 ++++++++++++++++++++++ scripts/conformance/rubyspec/FLOOR | 2 +- 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/internal/vm/array_edges_test.go b/internal/vm/array_edges_test.go index ff327558..48ac2407 100644 --- a/internal/vm/array_edges_test.go +++ b/internal/vm/array_edges_test.go @@ -176,3 +176,25 @@ func TestArrayCycle(t *testing.T) { } } } + +// TestArrayRindex covers Array#rindex — value form, block form (with the +// argument winning over a block), the no-argument sized Enumerator and the +// no-match nil. Asserted against MRI Ruby 4.0.6. +func TestArrayRindex(t *testing.T) { + cases := []struct{ src, want string }{ + {`p [1, 2, 3, 2, 1].rindex(2)`, "3\n"}, + {`p [1, 2, 3].rindex(9)`, "nil\n"}, + {`p [].rindex(1)`, "nil\n"}, + {`p [1, 2, 3, 2, 1].rindex { |x| x == 2 }`, "3\n"}, + {`p [1, 2, 3].rindex { |x| x > 5 }`, "nil\n"}, + {`p [1, 2, 3].rindex.class`, "Enumerator\n"}, + {`p [1, 2, 3, 2].rindex.to_a`, "[2, 3, 2, 1]\n"}, + {`e = [1, 2, 3, 2].rindex; p e.each { |x| x == 2 }`, "3\n"}, + {`p [1, 2, 3, 2, 1].rindex(2) { |x| x == 3 }`, "3\n"}, // argument wins over block + } + for _, c := range cases { + if got := eval(t, c.src); got != c.want { + t.Errorf("src=%q got=%q want=%q", c.src, got, c.want) + } + } +} diff --git a/internal/vm/builtins.go b/internal/vm/builtins.go index d7a66130..50839342 100644 --- a/internal/vm/builtins.go +++ b/internal/vm/builtins.go @@ -3270,6 +3270,28 @@ func (vm *VM) bootstrap() { } return object.NilV }) + // rindex searches backward from the end: rindex(obj) → the last index whose + // element == obj; rindex { |e| … } → the last index whose block is truthy (an + // argument, if given, wins over the block); no argument and no block → a sized + // Enumerator. Returns nil when nothing matches. + vm.cArray.define("rindex", func(vm *VM, self object.Value, args []object.Value, blk *Proc) object.Value { + a := self.(*object.Array) + if len(args) == 0 && blk == nil { + return enumForSized(self, "rindex", func(*VM) object.Value { return object.IntValue(int64(len(a.Elems))) }) + } + for i := len(a.Elems) - 1; i >= 0; i-- { + var match bool + if len(args) > 0 { + match = valueEqual(a.Elems[i], args[0]) + } else { + match = vm.callBlock(blk, []object.Value{a.Elems[i]}).Truthy() + } + if match { + return object.IntValue(int64(i)) + } + } + return object.NilV + }) vm.cArray.define("assoc", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { return vm.arrayAssoc(self.(*object.Array), args[0], 0) }) diff --git a/scripts/conformance/rubyspec/FLOOR b/scripts/conformance/rubyspec/FLOOR index adef6a43..5fa61811 100644 --- a/scripts/conformance/rubyspec/FLOOR +++ b/scripts/conformance/rubyspec/FLOOR @@ -1 +1 @@ -15820 +15830