Created
August 4, 2017 07:15
-
-
Save Sihui/d0e598f7902b9d53e954d7a982f9ca8e to your computer and use it in GitHub Desktop.
Design Pattern: Iterator and Movie Collections
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 ArrayIterator | |
attr_reader :collection | |
attr_accessor :current_index | |
def initialize(collection) | |
@collection = collection | |
@current_index = 0 | |
end | |
def has_next? | |
current_index < collection.length | |
end | |
def next | |
raise 'No more item left.' unless has_next? | |
item = collection[current_index] | |
self.current_index += 1 | |
item | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment