Created
March 10, 2012 01:58
-
-
Save takaheraw/2009696 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
def initialize(array) | |
@array = array | |
@index = 0 | |
end | |
def has_next? | |
@index < @array.length | |
end | |
def item | |
@array[@index] | |
end | |
def next_item | |
value = @array[@index] | |
@index += 1 | |
end | |
end | |
array = ["red","blue","green"] | |
i = ArrayIterator.new(array) | |
while i.has_next? | |
puts "item: #{i.next_item}" | |
end | |
i = ArrayIterator.new('abcd') | |
while i.has_next? | |
puts "item: #{i.next_item.chr}" | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment