Created
July 14, 2011 07:31
-
-
Save tomotaka/1082063 to your computer and use it in GitHub Desktop.
simple cyclic iterator(cannot be used on multithread environment)
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
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