Fiber可以让程序分段执行。
fiber.rb
fiber = Fiber.new do puts "before yield" Fiber.yield 1 puts "after yield" 2 end puts "before resume1" puts fiber.resume puts "before resume2" puts fiber.resume puts "before resume3" puts fiber.resume puts "after resume"
结果:
before resume1 before yield 1 before resume2 after yield 2 before resume3 fiber.rb:13:in `resume': dead fiber called (FiberError) from fiber.rb:13:in `<main>'