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
cont.c
View on GitHub ↗
static VALUE
rb_fiber_initialize(int argc, VALUE* argv, VALUE self)
{
return rb_fiber_initialize_kw(argc, argv, self, rb_keyword_given_p());
}
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
-
3.0
-
Fiber.new(blocking: true/false) allows you to create non-blocking execution contexts. [Feature #16786]
-