Here's how to use the new Fiber class (warning: class name may change) to generate an infinite sequence of Fibonacci numbers. I use "generate" in the sense of Python's generators. Ruby's new fibers are "semi-coroutines".

1 fib = Fiber.new do

2 x, y = 0, 1

3 loop do

4 Fiber.yield y

5 x,y = y,x+y

6 end

7 end

8 20.times { puts fib.resume }

Fiber.yield

yield

fib

fib.resume

Fiber.yield

Fiber.yield

y

return y

return

Fiber.yield

return

yield

resume

Fiber.yield

A fiber is a unit of execution that must be manually scheduled by the application. Fibers run in the context of the threads that schedule them. Each thread can schedule multiple fibers. In general, fibers do not provide advantages over a well-designed multithreaded application.However, using fibers can make it easier to port applications that were designed to schedule their own threads.

Sasada Koichi

YARV

These method names (resume/yield) are from Lua. "transfer" is from Modula-2. "double resume error" is from Python's generator. BTW, I'm thinking about name "Fiber". Current Fiber means Semi-Coroutine. Fiber::Core is Coroutine. Yes, name of Fiber is from Microsoft, but it's means Semi-Coroutine such as Lua's coroutine and Python's generator.

Semi-Coroutines

asymmetric Coroutines

back

Generator

Semi-Coroutine (Fiber)

Coroutine (Fiber::Core)

It is still hot topic among core developers. But fibers (and external iterators) are likely to remain in the final 1.9, more likely than continuations.

lightweight concurrency

In modern concurrency settings they [Fibers] are becoming increasingly useful, however. Without them, or something like them (e.g. Rubinius Tasks), you must play some very ugly games to get lightweight concurrency -- see the use of explicit continuation-passing (functions, not Continuations) in Scala's actors library for an example of the best that can be hoped for in their absence.

Granted, Fibers will make things harder for JRuby