kjana / os.xaxon.ne.jp (YANAGAWA Kazuhisa) writes: > In message <m2n1n5lwjf.fsf / zip.local.thomases.com> > Dave / thomases.com writes: > > > Given an iterator method, how can I get the results expressed as an > > array? I could do: > > > > result = [] > > iteratorMethod { |i| result << i } > > If your iterator is defined on a collection includes Enumerable > (eg. Array), you can use --- what is one subject these days --- > Enumerable#collect. Or you prefer a method like Array#filter for its > effectiveness. > > # Or do I miss the point? Say I have a method which generates Fibonacci numbers to to a limit: def fibUpTo(max) i1, i2 = 1, 1 while i1 <= max yield i1 i1, i2 = i2, i1+i2 end end I can do something like result = [] fibUpTo(20) { |f| result << f } to collect the results into an array. However, to use Enumerable, wouldn't I have to wrap it all in a class? class FibGen include Enumerable def initialize(max) @max = max end def each i1, i2 = 1, 1 while i1 <= @max yield i1 i1, i2 = i2, i1+i2 end end end result = FibGen.new(20).collect This just seems like a lot of work (he said, lazily). It would be nice to be able to use the iterator method directly to achieve the same thing. Dave