How can i iterate over a ruby array and always get two values, the current and the next, like:
[1,2,3,4,5,6].pairwise do |a,b|
# a=1, b=2 in first iteration
# a=2, b=3 in second iteration
# a=3, b=4 in third iteration
# ...
# a=5, b=6 in last iteration
end
My usecase: I want to test if an array is sorted, and by having an iterator like this i could always compare two values.
I am not searching for each_slice
like in this question: Converting ruby array to array of consecutive pairs