Fibers
Cooperative concurrency.
Fibers is a free Ruby Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is a Fiber?
A Fiber is a lightweight unit of execution you control by hand. Unlike threads, a fiber never runs on its own; you explicitly hand control to it and it hands control back.
- This is called cooperative concurrency.
- Fibers are cheap to create and pause.
- They are the foundation of generators and async I/O in Ruby.
Creating and Resuming
Create a fiber with Fiber.new and start or continue it with resume. The block runs until it finishes or pauses.
fiber = Fiber.new do
puts "Inside the fiber"
end
fiber.resume
puts "Back in main"Pausing with yield
Fiber.yield pauses the fiber and returns control to whoever called resume. The next resume continues right after the yield.
fiber = Fiber.new do
puts "Step 1"
Fiber.yield
puts "Step 2"
end
fiber.resume
puts "Main between steps"
fiber.resumePassing Values Out
The argument to Fiber.yield becomes the return value of resume. This lets a fiber produce a sequence of values on demand.
fiber = Fiber.new do
Fiber.yield 10
Fiber.yield 20
30
end
puts fiber.resume
puts fiber.resume
puts fiber.resumePassing Values In
The argument to resume becomes the return value of Fiber.yield inside the fiber. Communication flows both ways.
fiber = Fiber.new do
got = Fiber.yield "ready"
puts "Fiber received: " + got
end
puts fiber.resume
fiber.resume("hello")An Infinite Counter
Because a fiber pauses and resumes, it can model an endless lazy sequence without ever computing everything.
counter = Fiber.new do
n = 1
loop do
Fiber.yield n
n += 1
end
end
3.times { puts counter.resume }Checking If Alive
Fiber#alive? tells you whether the fiber can still be resumed. Resuming a dead fiber raises a FiberError.
fiber = Fiber.new { puts "done" }
puts "Alive before: " + fiber.alive?.to_s
fiber.resume
puts "Alive after: " + fiber.alive?.to_sFibers vs Threads
Key differences:
- Threads are scheduled by the runtime; fibers are scheduled by you.
- Threads can be preempted at any time; fibers only switch at
yieldorresume. - Fibers avoid race conditions because only one runs and switching points are explicit.
Enumerators Use Fibers
Ruby's lazy enumerators are built on fibers. An external iterator pauses between values just like a fiber yields.
enum = Enumerator.new do |y|
y << 1
y << 2
y << 3
end
puts enum.next
puts enum.nextFibers and Async I/O
Modern Ruby includes a fiber scheduler hook. Libraries like the async gem use it so that when one fiber waits on I/O, the scheduler resumes another fiber automatically.
This gives high-concurrency I/O with code that reads like ordinary sequential code.
When to Use Fibers
Reach for fibers when you need:
- Lazy generators producing values on demand.
- Coroutine-style control flow.
- Fine-grained, deterministic switching without locks.
For most everyday concurrency, threads or the async gem (which uses fibers under the hood) are easier.
Quick Check
Test your understanding of fibers.
Recap: Fibers
You learned cooperative concurrency with fibers:
Fiber.newplusresumeruns and continues a fiber.Fiber.yieldpauses and returns control.- Values flow both ways via yield and resume arguments.
- Fibers power enumerators and async I/O schedulers.
Next: Ractors, which finally give you true parallelism.
Frequently asked questions
Is the “Fibers” lesson free?
Yes — the full text of “Fibers” is free to read here on the web, and the Ruby Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ruby Academy course, upgrade to CoddyKit PRO.
What will I learn in “Fibers”?
Cooperative concurrency. You practise Ruby Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Ruby Academy?
No prior experience is required. Ruby Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Fibers” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Ruby Academy lesson?
Yes. Every Ruby Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.