From b02b90048d4c351ece2c6e0306011a288183b560 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Tue, 25 Aug 2026 13:33:54 +0200 Subject: [PATCH] Add Enumerable#reverse_each Enumerable#reverse_each was undefined (NoMethodError). It is now defined in the prelude: with a block it buffers the elements through #to_a (so a multi-value each gathers each yield into an array) and passes them to the block in reverse order, returning the receiver; with no block it returns an Enumerator whose size is the receiver's own #size when it defines one and nil otherwise. Verified against ruby 4.0.6. Full package coverage stays at 100.0% (the frozen prelude was regenerated). Zero-regression rubyspec sweep; core/enumerable/reverse_each_spec 0 -> 5. Co-Authored-By: Claude Opus 4.8 --- .../enumerable_reverse_each_internal_test.go | 37 +++++++++++++++++++ internal/vm/prelude.rb | 10 +++++ internal/vm/prelude_frozen_gen.go | 35 +++++++++++++++++- 3 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 internal/vm/enumerable_reverse_each_internal_test.go diff --git a/internal/vm/enumerable_reverse_each_internal_test.go b/internal/vm/enumerable_reverse_each_internal_test.go new file mode 100644 index 00000000..1cdc880e --- /dev/null +++ b/internal/vm/enumerable_reverse_each_internal_test.go @@ -0,0 +1,37 @@ +// Copyright (c) the go-embedded-ruby/ruby authors +// +// SPDX-License-Identifier: BSD-3-Clause + +package vm + +import "testing" + +// TestEnumerableReverseEach covers Enumerable#reverse_each: with a block it +// yields the elements in reverse and returns the receiver; a multi-value each +// gathers each yield into an array; with no block it returns an Enumerator whose +// size is the receiver's own size when it defines one and nil otherwise. Verified +// against ruby 4.0.6. +func TestEnumerableReverseEach(t *testing.T) { + const numerous = `class E; include Enumerable; def each; yield 2; yield 5; yield 3; yield 6; yield 1; yield 4; end; end; ` + const withSize = `class S; include Enumerable; def each; yield 1; yield 2; yield 3; yield 4; end; def size; 4; end; end; ` + const multi = `class M; include Enumerable; def each; yield 1, 2; yield 3, 4, 5; end; end; ` + cases := []struct{ src, want string }{ + // Block form: reverse order, returns the receiver. + {numerous + `a = []; E.new.reverse_each { |i| a << i }; p a`, `[4, 1, 6, 3, 5, 2]`}, + {numerous + `p E.new.reverse_each {}.class`, `E`}, + // No block: an Enumerator that reverses. + {numerous + `p E.new.reverse_each.class`, `Enumerator`}, + {numerous + `p E.new.reverse_each.to_a`, `[4, 1, 6, 3, 5, 2]`}, + // A multi-value each gathers each yield into an array, then reverses. + {multi + `y = []; M.new.reverse_each { |e| y << e }; p y`, `[[3, 4, 5], [1, 2]]`}, + // The enumerator's size follows the receiver: known when it has #size... + {withSize + `p S.new.reverse_each.size`, `4`}, + // ...and nil otherwise. + {numerous + `p E.new.reverse_each.size`, `nil`}, + } + for _, c := range cases { + if got := eval(t, c.src); got != c.want+"\n" { + t.Errorf("src=%q got=%q want=%q", c.src, got, c.want+"\n") + } + } +} diff --git a/internal/vm/prelude.rb b/internal/vm/prelude.rb index c7645134..0fc96240 100644 --- a/internal/vm/prelude.rb +++ b/internal/vm/prelude.rb @@ -438,6 +438,16 @@ def each_with_index self end + # reverse_each buffers the elements (through to_a, so a multi-value each + # gathers each yield into an array) and passes them to the block in reverse + # order, returning the receiver. With no block it returns an Enumerator whose + # size is the receiver's own size when it has one, and nil otherwise. + def reverse_each(&block) + return enum_for(:reverse_each) { respond_to?(:size) ? size : nil } unless block + to_a.reverse_each(&block) + self + end + def flat_map return enum_for(:flat_map) unless block_given? r = [] diff --git a/internal/vm/prelude_frozen_gen.go b/internal/vm/prelude_frozen_gen.go index b36fddcf..65eaff63 100644 --- a/internal/vm/prelude_frozen_gen.go +++ b/internal/vm/prelude_frozen_gen.go @@ -181,13 +181,13 @@ func embeddedPrelude() *bytecode.ISeq { }, &bytecode.ISeq{ Name: "", - Insns: []bytecode.Instr{{Op: 44}, {Op: 9}, {Op: 44, A: 1, B: 1}, {Op: 9}, {Op: 44, A: 2, B: 2}, {Op: 9}, {Op: 5}, {Op: 1}, {Op: 1, A: 1}, {Op: 1, A: 2}, {Op: 30, A: 3, B: 3}, {Op: 9}, {Op: 44, A: 4, B: 3}, {Op: 9}, {Op: 44, A: 5, B: 4}, {Op: 9}, {Op: 44, A: 6, B: 5}, {Op: 9}, {Op: 44, A: 7, B: 6}, {Op: 9}, {Op: 44, A: 8, B: 7}, {Op: 9}, {Op: 44, A: 9, B: 8}, {Op: 9}, {Op: 44, A: 10, B: 9}, {Op: 9}, {Op: 44, A: 11, B: 10}, {Op: 9}, {Op: 44, A: 12, B: 11}, {Op: 9}, {Op: 44, A: 13, B: 12}, {Op: 9}, {Op: 44, A: 14, B: 13}, {Op: 9}, {Op: 44, A: 15, B: 14}, {Op: 9}, {Op: 44, A: 16, B: 15}, {Op: 9}, {Op: 44, A: 17, B: 16}, {Op: 9}, {Op: 44, A: 18, B: 17}, {Op: 9}, {Op: 44, A: 19, B: 18}, {Op: 9}, {Op: 44, A: 20, B: 19}, {Op: 9}, {Op: 44, A: 21, B: 20}, {Op: 9}, {Op: 44, A: 22, B: 21}, {Op: 9}, {Op: 44, A: 23, B: 22}, {Op: 9}, {Op: 44, A: 24, B: 23}, {Op: 9}, {Op: 44, A: 25, B: 24}, {Op: 9}, {Op: 44, A: 26, B: 25}, {Op: 9}, {Op: 44, A: 27, B: 26}, {Op: 9}, {Op: 44, A: 28, B: 27}, {Op: 9}, {Op: 44, A: 29, B: 28}, {Op: 9}, {Op: 44, A: 30, B: 29}, {Op: 9}, {Op: 44, A: 31, B: 30}, {Op: 9}, {Op: 44, A: 32, B: 31}, {Op: 9}, {Op: 44, A: 33, B: 32}, {Op: 9}, {Op: 44, A: 34, B: 33}, {Op: 9}, {Op: 44, A: 35, B: 34}, {Op: 9}, {Op: 44, A: 36, B: 35}, {Op: 9}, {Op: 44, A: 37, B: 36}, {Op: 9}, {Op: 44, A: 38, B: 37}, {Op: 9}, {Op: 44, A: 39, B: 38}, {Op: 9}, {Op: 44, A: 40, B: 39}, {Op: 9}, {Op: 44, A: 41, B: 40}, {Op: 9}, {Op: 44, A: 42, B: 41}, {Op: 9}, {Op: 44, A: 43, B: 42}, {Op: 9}, {Op: 44, A: 44, B: 43}, {Op: 9}, {Op: 44, A: 45, B: 44}, {Op: 9}, {Op: 44, A: 46, B: 45}, {Op: 9}, {Op: 44, A: 47, B: 46}, {Op: 9}, {Op: 44, A: 48, B: 47}, {Op: 9}, {Op: 44, A: 49, B: 48}, {Op: 9}, {Op: 44, A: 50, B: 49}, {Op: 9}, {Op: 44, A: 51, B: 50}, {Op: 9}, {Op: 44, A: 52, B: 51}, {Op: 9}, {Op: 44, A: 53, B: 52}, {Op: 9}, {Op: 44, A: 54, B: 53}, {Op: 9}, {Op: 44, A: 55, B: 54}, {Op: 9}, {Op: 44, A: 56, B: 55}, {Op: 9}, {Op: 44, A: 57, B: 56}, {Op: 9}, {Op: 44, A: 58, B: 57}, {Op: 9}, {Op: 44, A: 59, B: 58}, {Op: 53}}, + Insns: []bytecode.Instr{{Op: 44}, {Op: 9}, {Op: 44, A: 1, B: 1}, {Op: 9}, {Op: 44, A: 2, B: 2}, {Op: 9}, {Op: 5}, {Op: 1}, {Op: 1, A: 1}, {Op: 1, A: 2}, {Op: 30, A: 3, B: 3}, {Op: 9}, {Op: 44, A: 4, B: 3}, {Op: 9}, {Op: 44, A: 5, B: 4}, {Op: 9}, {Op: 44, A: 6, B: 5}, {Op: 9}, {Op: 44, A: 7, B: 6}, {Op: 9}, {Op: 44, A: 8, B: 7}, {Op: 9}, {Op: 44, A: 9, B: 8}, {Op: 9}, {Op: 44, A: 10, B: 9}, {Op: 9}, {Op: 44, A: 11, B: 10}, {Op: 9}, {Op: 44, A: 12, B: 11}, {Op: 9}, {Op: 44, A: 13, B: 12}, {Op: 9}, {Op: 44, A: 14, B: 13}, {Op: 9}, {Op: 44, A: 15, B: 14}, {Op: 9}, {Op: 44, A: 16, B: 15}, {Op: 9}, {Op: 44, A: 17, B: 16}, {Op: 9}, {Op: 44, A: 18, B: 17}, {Op: 9}, {Op: 44, A: 19, B: 18}, {Op: 9}, {Op: 44, A: 20, B: 19}, {Op: 9}, {Op: 44, A: 21, B: 20}, {Op: 9}, {Op: 44, A: 22, B: 21}, {Op: 9}, {Op: 44, A: 23, B: 22}, {Op: 9}, {Op: 44, A: 24, B: 23}, {Op: 9}, {Op: 44, A: 25, B: 24}, {Op: 9}, {Op: 44, A: 26, B: 25}, {Op: 9}, {Op: 44, A: 27, B: 26}, {Op: 9}, {Op: 44, A: 28, B: 27}, {Op: 9}, {Op: 44, A: 29, B: 28}, {Op: 9}, {Op: 44, A: 30, B: 29}, {Op: 9}, {Op: 44, A: 31, B: 30}, {Op: 9}, {Op: 44, A: 32, B: 31}, {Op: 9}, {Op: 44, A: 33, B: 32}, {Op: 9}, {Op: 44, A: 34, B: 33}, {Op: 9}, {Op: 44, A: 35, B: 34}, {Op: 9}, {Op: 44, A: 36, B: 35}, {Op: 9}, {Op: 44, A: 37, B: 36}, {Op: 9}, {Op: 44, A: 38, B: 37}, {Op: 9}, {Op: 44, A: 39, B: 38}, {Op: 9}, {Op: 44, A: 40, B: 39}, {Op: 9}, {Op: 44, A: 41, B: 40}, {Op: 9}, {Op: 44, A: 42, B: 41}, {Op: 9}, {Op: 44, A: 43, B: 42}, {Op: 9}, {Op: 44, A: 44, B: 43}, {Op: 9}, {Op: 44, A: 45, B: 44}, {Op: 9}, {Op: 44, A: 46, B: 45}, {Op: 9}, {Op: 44, A: 47, B: 46}, {Op: 9}, {Op: 44, A: 48, B: 47}, {Op: 9}, {Op: 44, A: 49, B: 48}, {Op: 9}, {Op: 44, A: 50, B: 49}, {Op: 9}, {Op: 44, A: 51, B: 50}, {Op: 9}, {Op: 44, A: 52, B: 51}, {Op: 9}, {Op: 44, A: 53, B: 52}, {Op: 9}, {Op: 44, A: 54, B: 53}, {Op: 9}, {Op: 44, A: 55, B: 54}, {Op: 9}, {Op: 44, A: 56, B: 55}, {Op: 9}, {Op: 44, A: 57, B: 56}, {Op: 9}, {Op: 44, A: 58, B: 57}, {Op: 9}, {Op: 44, A: 59, B: 58}, {Op: 9}, {Op: 44, A: 60, B: 59}, {Op: 53}}, Consts: []object.Value{ object.Symbol("__each_packed"), object.Symbol("__pack"), object.Symbol("__enum_int_arg"), }, - Names: []string{"__each_packed", "__pack", "__enum_int_arg", "private", "to_a", "to_set", "to_h", "map", "collect", "filter", "detect", "count", "min_by", "max_by", "sort", "sort_by", "select", "reject", "find", "include?", "sum", "min", "max", "minmax", "reduce", "inject", "any?", "all?", "none?", "one?", "uniq", "compact", "each_with_index", "flat_map", "collect_concat", "each_entry", "each_with_object", "filter_map", "partition", "group_by", "tally", "zip", "find_index", "find_all", "grep", "grep_v", "take_while", "drop_while", "each_slice", "each_cons", "first", "take", "drop", "chunk_while", "slice_when", "chunk", "slice_before", "slice_after", "minmax_by", "cycle"}, + Names: []string{"__each_packed", "__pack", "__enum_int_arg", "private", "to_a", "to_set", "to_h", "map", "collect", "filter", "detect", "count", "min_by", "max_by", "sort", "sort_by", "select", "reject", "find", "include?", "sum", "min", "max", "minmax", "reduce", "inject", "any?", "all?", "none?", "one?", "uniq", "compact", "each_with_index", "reverse_each", "flat_map", "collect_concat", "each_entry", "each_with_object", "filter_map", "partition", "group_by", "tally", "zip", "find_index", "find_all", "grep", "grep_v", "take_while", "drop_while", "each_slice", "each_cons", "first", "take", "drop", "chunk_while", "slice_when", "chunk", "slice_before", "slice_after", "minmax_by", "cycle"}, NumRequired: 0, SplatIndex: -1, KwRestSlot: -1, @@ -1123,6 +1123,37 @@ func embeddedPrelude() *bytecode.ISeq { }, Super: "", }, + &bytecode.ISeq{ + Name: "reverse_each", + Insns: []bytecode.Instr{{Op: 11}, {Op: 25}, {Op: 28, A: 8}, {Op: 5}, {Op: 1}, {Op: 30, B: 1, C: 1}, {Op: 53}, {Op: 26, A: 9}, {Op: 2}, {Op: 9}, {Op: 5}, {Op: 30, A: 1}, {Op: 11}, {Op: 66, A: 2, Flags: 1}, {Op: 9}, {Op: 5}, {Op: 53}}, + Consts: []object.Value{ + object.Symbol("reverse_each"), + }, + Names: []string{"enum_for", "to_a", "reverse_each"}, + NumRequired: 0, + SplatIndex: -1, + KwRestSlot: -1, + BlockSlot: 0, + NumLocals: 1, + Locals: []string{"block"}, + Children: []*bytecode.ISeq{ + &bytecode.ISeq{ + Name: "", + Insns: []bytecode.Instr{{Op: 5}, {Op: 1}, {Op: 30, B: 1}, {Op: 28, A: 7}, {Op: 5}, {Op: 30, A: 1}, {Op: 26, A: 8}, {Op: 2}, {Op: 53}}, + Consts: []object.Value{ + object.Symbol("size"), + }, + Names: []string{"respond_to?", "size"}, + NumRequired: 0, + SplatIndex: -1, + KwRestSlot: -1, + BlockSlot: -1, + NumLocals: 0, + Super: "", + }, + }, + Super: "", + }, &bytecode.ISeq{ Name: "flat_map", Insns: []bytecode.Instr{{Op: 52}, {Op: 25}, {Op: 28, A: 8}, {Op: 5}, {Op: 1}, {Op: 30, B: 1}, {Op: 53}, {Op: 26, A: 9}, {Op: 2}, {Op: 9}, {Op: 6}, {Op: 12}, {Op: 9}, {Op: 5}, {Op: 30, A: 1, C: 1}, {Op: 9}, {Op: 11}, {Op: 53}},