Array Iterator
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
1. | Creating Composites | ||
2. | Sprucing Up the Composite with Operators | ||
3. | Composite with Push and index Operators | ||
4. | An Array as a Composite |