From: Alan Chen Date: 2002-09-13T15:10:10+09:00 Subject: Re: General ?s about a couple of Ruby features On Fri, Sep 13, 2002 at 04:40:24AM +0900, Paul Brannan wrote: > > * Regarding iterators: it is clearly often more elegant to use an > > iterator rather than a for or while loop. Does the existence of > > iterators provide any benefit to end users (e.g. performance)? > > The for keyword is implemented by calling each(), so in order for it to > work, an each() method must be defined. Note that perl has foreach which does a very similiar thing as ruby. The difference with ruby is that defining each and then mixing in module Enumerable, lets the user written classes support all of the standard iterator calls. If you implement <=> you get a few more comparison based calls too. (I learned this from Programming Ruby -- thanks Dave.) > The advantage of iterators is that you separate the iterating code from > what is done to each element. It doesn't matter whether you are > iterating over an array or a range or a binary tree; the syntax is > exactly the same. > > Paul > -- Alan Chen Digikata LLC http://digikata.com