Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
aimee daniells
committed
Mar 30, 2010
1 parent
6d3766f
commit 459b003
Showing
2 changed files
with
11 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
h2. Iterator | ||
|
||
bq. Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation. | ||
|
||
So we all know that Ruby provides a very neat internal iterator using @Enumerable@ and the @each@ method which takes a block. We have no control over where and when the internal iterator moves its own index on; it just figures it out by itself. | ||
|
||
To make an external iterator i defined @first@, @next@ and @done?@ methods for a ListIterator class. The client (which in this case happens to be the test) is responsible for moving the index via those exposed methods. The client does not need to know where the index is currently: we can quite easily define a ReverseIterator which goes through the list backwards. As far as the client is concerned, that doesn't matter, but the iterator provides a useful way of accessing the items in the list. | ||
|
||
Even though an array can easily be traversed using Ruby's built in internal iterator, i decided to make my own version of an internal iterator anyway, to make sure i understood it. I called the method @each_item@ and i figured out that the internal iterator needed to accept a block, yield the item at its current index in order to perform the block, and move its index on. |