Ruby Academy · Lesson

Fibers

Cooperative concurrency.

Lesson 3 of 413 steps

Fibers is a free Ruby Academy lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, and your progress syncs across the web and the CoddyKit app. The Ruby Academy course includes 4 lessons in total.

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.resume

Passing 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.resume

Passing 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_s

Fibers 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 yield or resume.
  • 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.next

Fibers 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.new plus resume runs and continues a fiber.
  • Fiber.yield pauses 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.

Free to start

Learn Ruby with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
29
Lessons
109

Frequently Asked Questions

Is the “Fibers” lesson free?

Yes — any 3 lessons of the Ruby Academy learning path, including “Fibers”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Ruby Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.

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.

All lessons in this course

  1. Threads
  2. The GVL Explained
  3. Fibers
  4. Ractors
← Back to Ruby Academy