Skip to content

End of Life Ruby 3.1 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

class method

Fiber.new

Fiber.new(blocking: false) { |*args| ... } → fiber

Creates new Fiber. Initially, the fiber is not running and can be resumed with resume. Arguments to the first resume call will be passed to the block:

Example
f = Fiber.new do |initial|
   current = initial
   loop do
     puts "current: #{current.inspect}"
     current = Fiber.yield
   end
end
f.resume(100)     # prints: current: 100
f.resume(1, 2, 3) # prints: current: [1, 2, 3]
f.resume          # prints: current: nil
# ... and so on ...

If blocking: false is passed to Fiber.new, and current thread has a Fiber.scheduler defined, the Fiber becomes non-blocking (see “Non-blocking Fibers” section in class docs).

Ruby version changes

  1. 3.0
See also .current_scheduler .schedule .current .scheduler also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.0