reverse_each_with_index配列のようなものを使用したいと思います。
例:
array.reverse_each_with_index do |node,index|
puts node
puts index
end
Rubyにはeach_with_indexあるようですが、反対のものはないようです。これを行う別の方法はありますか?
reverse_each_with_index配列のようなものを使用したいと思います。
例:
array.reverse_each_with_index do |node,index|
puts node
puts index
end
Rubyにはeach_with_indexあるようですが、反対のものはないようです。これを行う別の方法はありますか?
最初reverseに配列を作成してから、次を使用しますeach_with_index。
array.reverse.each_with_index do |element, index|
# ...
end
ただし、インデックスは から0に移動しますがlength - 1、その逆ではありません。
Ruby は常にオプションを提供するのが好きなので、できることは次のとおりです。
arr.reverse.each_with_index do |e, i|
end
しかし、次のこともできます:
arr.reverse_each.with_index do |e, i|
end
単に
arr.reverse.each_with_index do |node, index|