Last active
August 29, 2015 14:01
-
-
Save mark/b216c0fdd9594499967e to your computer and use it in GitHub Desktop.
Enumerator # pair
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module PairEnumerator | |
def pair | |
if block_given? | |
if any? | |
last_item = self.next | |
each do |this_item| | |
result = yield(last_item, this_item) | |
last_item = this_item | |
result | |
end | |
end | |
else | |
Enumerator.new do |y| | |
pair { |last_item, this_item| y.<<(last_item, this_item) } | |
end | |
end | |
end | |
end | |
class Enumerator | |
include PairEnumerator | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment