If you want the real index of the element in the array you can do this
['Seriously', 'Chunky', 'Bacon'].to_enum.with_index.reverse_each do |word, index|
puts "index #{index}: #{word}"
end
Output:
index 2: Bacon
index 1: Chunky
index 0: Seriously
You can also define your own reverse_each_with_index method
class Array
def reverse_each_with_index &block
to_enum.with_index.reverse_each &block
end
end
['Seriously', 'Chunky', 'Bacon'].reverse_each_with_index do |word, index|
puts "index #{index}: #{word}"
end
An optimized version
class Array
def reverse_each_with_index &block
(0...length).reverse_each do |i|
block.call self[i], i
end
end
end
reverse_each_with_index, or want to implement one that has a certain function? Different portions of your question imply one or the other.