Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions internal/vm/enumerable_reverse_each_internal_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
}
10 changes: 10 additions & 0 deletions internal/vm/prelude.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down
35 changes: 33 additions & 2 deletions internal/vm/prelude_frozen_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.