I want to loop over an array like [:foo, :bar, :baz].
On each iteration, I want the item and the next item. On the last iteration, the "next item" should be nil.
So I want to yield in turn :foo, :bar, then :bar, :baz, and finally :baz, nil.
I can think of a few ways of achieving this:
my_list.zip(my_list.from(1)) { |item, next_item| … }my_list.each.with_index(1) { |item, i| next_item = my_list[i] }[*my_list, nil].each_cons(2) { |item, next_item| … }
But I feel like I might be missing some simpler way. Am I?