Skip to content

Instantly share code, notes, and snippets.

@Sihui
Created August 4, 2017 07:15
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 Sihui/d0e598f7902b9d53e954d7a982f9ca8e to your computer and use it in GitHub Desktop.
Save Sihui/d0e598f7902b9d53e954d7a982f9ca8e to your computer and use it in GitHub Desktop.
Design Pattern: Iterator and Movie Collections
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