Skip to content

Instantly share code, notes, and snippets.

@takaheraw
Created March 10, 2012 01:58
Show Gist options
  • Save takaheraw/2009696 to your computer and use it in GitHub Desktop.
Save takaheraw/2009696 to your computer and use it in GitHub Desktop.
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