From: Csaba Henk Date: 2005-06-20T20:18:05+09:00 Subject: Re: Iterate with condition On Mon, Jun 20, 2005 at 07:52:30PM +0900, G?bor SEBESTY?N wrote: > Hi, > > I need but actually don't find how to iterate an iterable collection > in Java-way. It seems generally like this: > > Iterator it = objs.iterator(); > while (it.hasMore()) { > Object anObj = it.next(); > ... > } > > What is the equivalent to this in Ruby? By some lingo "Java stlyle" iterators are called "external", "Ruby style" ones are called "internal". I make this note just to separate languages from concepts... So, Ruby has built-in support for only for internal iterators, but ruby also features callcc, which makes it easy to create external ones, too. The generator package (part of the standard lib) implements this concept. While this approach is pretty elegant, the downside is a massive performance loss. Thus you might consider using a dumb solution, if there is one. Eg, a typical case is when you want to iterate through two arrays simultaneously -- then, unless you have huge arrays, it would be faster to create a joint array by Array#zip or Array#transpose, and use its each method, or just dup the arrays and shfit them. Csaba