From: Clemens Hintze Date: 2001-10-14T17:43:20+09:00 Subject: [ruby-talk:22508] ANN: Generator -- a class supporting lazy evaluation (Re: Open-ended ranges) Robert Feldt wrote: (...) > Ruby doesn't have infinite or unbounded data (by default) structs so what > should this mean? We would have to add Inf (might be good for other > situations) and introduce Object#default_max_value, but how to handle > > (1..).to_a > > without going for lazy evaluation? (...) Robert, sorry for mis-using your post, but the opportunity is too nice to let it pass :-) There is a new class in town ... Generator. Generator is a class that supports you to execute associated code blocks only partially by suspending itself while returning an result to the caller. Suspending means, that the whole state of the code block will be stored in a Continuation instance. Next invocation of a Generator instance will back up where it was left previously, ready to compute and return another value when suspending again. Code could looks like this: arr = ["Ruby", "Chan", "rules"] igen = Generator.new { arr.each { |el| suspend el } } puts igen.next #-> Ruby puts igen.next #-> Chan puts igen.next #-> rules The more comprehensive documentation is stored together with the source code in RD format. Let me know, if you like this gimmick or, of course, if you find any errors! You may obtain it from RAA: http://www.ruby-lang.org/en/raa-list.rhtml?name=Generator Much fun, \cle