Quick one today because today is a family day. Enumerable#each_cons() is a cool method which singles out consecutive elements from a collection.

This is straight from the Ruby docs.

(1..10).each_cons(3) { |a| p a }
# outputs below
[1, 2, 3]
[2, 3, 4]
[3, 4, 5]
[4, 5, 6]
[5, 6, 7]
[6, 7, 8]
[7, 8, 9]
[8, 9, 10]

:)

Leave a Reply

Your email address will not be published. Required fields are marked *