Skip to content

Implements #reverse_each in Literal::Array #299

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions lib/literal/array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,11 @@ def reverse(...)

alias_method :initialize_copy, :replace

def reverse_each(...)
return_value = @__value__.reverse_each(...)
(return_value.class == Enumerator) ? return_value : self
end

def reverse!
@__value__.reverse!
self
Expand Down
18 changes: 18 additions & 0 deletions test/array.test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,24 @@
end
end

test "#reverse_each iterates through the array in reverse" do
array = Literal::Array(Integer).new(1, 2, 3)
results = []

return_value = array.reverse_each { |i| results << i }

assert_same return_value, array
assert_equal results, [3, 2, 1]
end

test "#reverse_each returns an enumerator if no block is given" do
array = Literal::Array(Integer).new(1, 2, 3)

return_value = array.reverse_each

assert_equal return_value.class, Enumerator
end

test "#values_at returns the values at the given indexes" do
array = Literal::Array(Integer).new(1, 2, 3)

Expand Down
Loading