From: Peter Zotov Date: 2011-05-18T21:14:17+09:00 Subject: Re: Making fibers enumerable On Wed, 18 May 2011 20:12:43 +0900, Robert Klemme wrote: > How would your example look if there was another generation that you > wanted to do concurrently? Because for the single threaded generator > case there is already a tool: Enumerator.new. The example in the > documentation even uses Fibonacci Numbers as example. :-) > > http://www.ruby-doc.org/core/classes/Enumerator.html#M000299 > > fib_gen = Enumerator.new { |y| > a = b = 1 > loop { > y << a > a, b = b, a + b > } > } > > Now you can do exactly the same as you did with your example > > # Find the fibonacci number greater than 1000 > fib_gen.find {|x| x > 1000 } > # take first 10 fibonacci numbers > fib_gen.take 10 > # take_while numbers are smaller than 1000 > fib_gen.take_while {|x| x < 1000 } > > For this Fiber would be the wrong tool. The Enumerator.new uses Fibers internally. Check enumerator.c in the sources; besides that, how would you achieve the needed effect without coroutines? -- WBR, Peter Zotov.