From: Ned Konz Date: 2001-10-02T01:56:08+09:00 Subject: [ruby-talk:21876] Re: Python generators On Monday 01 October 2001 09:28 am, Henning von Rosen wrote: > Can anyone say two sentences of how the Python generator/iterator concept > maps onto Ruby? I'm no Ruby expert, but it seems to me (please correct me if I'm wrong) (and it's more than two sentences): Any Ruby method call can have an associated block argument. Within the called method, this block can be invoked with the yield() operator. Since Ruby blocks can be closures (that is, they capture variable bindings), this allows them to maintain state for use as iterators or generators. Beyond this basic support, there is a mixed-in module called Enumerable that provides a number of collection utility methods (collect, detect, each_with_index, entries, find, find_all, grep, include?, map, member?, reject, and select) using a generator method called each(), which when called with a block argument, passes each of the elements of the sequence to the block in turn. collection.each { |element| ... } So if you want to work with the Enumerable interface, just provide a generator called each() and mix in Enumerable. -- Ned Konz currently: Stanwood, WA email: ned@bike-nomad.com homepage: http://bike-nomad.com