Skip to content

Instantly share code, notes, and snippets.

@mark
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mark/b216c0fdd9594499967e to your computer and use it in GitHub Desktop.
Save mark/b216c0fdd9594499967e to your computer and use it in GitHub Desktop.
Enumerator # pair
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