Skip to content

Instantly share code, notes, and snippets.

@tomotaka
Created July 14, 2011 07:31
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 tomotaka/1082063 to your computer and use it in GitHub Desktop.
Save tomotaka/1082063 to your computer and use it in GitHub Desktop.
simple cyclic iterator(cannot be used on multithread environment)
class CyclicIterator
def initialize(collection)
@collection = collection
@i = 0
end
def next
item = nil
item = @collection[@i]
@i += 1
@i = 0 if @i == @collection.size
return item
end
end
# c = CyclicIterator.new([1, 2, 3])
# p c.next #=> 1
# p c.next #=> 2
# p c.next #=> 3
# p c.next #=> 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment