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
value
end
end
array = ['red', 'green', 'blue']
i = ArrayIterator.new(array)
while i.has_next?
puts("item: #{i.next_item}")
end
# Running this code will give us the output we expect:
# item: red
# item: green
# item: blue