Skip to content

Instantly share code, notes, and snippets.

@jtprince
jtprince / nil_enumerator.rb
Created June 8, 2012 18:47
NilEnumerator: an enumerator that returns nil instead of a StopIteration exception when it is at the end of the collection
# https://gist.github.com/gists/2897543
# NilEnumerator
#
# an enumerator that yields nil when it is finished. Only implements #next and
# #peak. Would only want to use this if your collection does NOT already include
# nils.
#
# Compare:
#
# # normal iteration requires catching StopIteration