私がこのようなことをするつもりなら
array.each_index do |i|
if array[i] > array[i + i]
#something
end
end
インデックスが範囲外になるのを防ぐためにこれを変更するにはどうすればよいですか?
私がこのようなことをするつもりなら
array.each_index do |i|
if array[i] > array[i + i]
#something
end
end
インデックスが範囲外になるのを防ぐためにこれを変更するにはどうすればよいですか?
array.each_cons(2) do |current, nekst|
if current > nekst
#something
end
end
または、インデックスが必要な場合は、次のようにします。
array.each_cons(2).with_index do |(current, nekst), i|
if current > nekst
#something with i
end
end
array.each_index do |i|
if array[i + 1] and array[i] > array[i + i]
#something
end
end