Challenge - 5 Problems
Fiber Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of simple Fiber switch
What is the output of this Ruby code using Fiber for cooperative concurrency?
Ruby
fiber = Fiber.new do puts "Step 1" Fiber.yield puts "Step 2" end fiber.resume puts "Between resumes" fiber.resume
Attempts:
2 left
💡 Hint
Remember that Fiber.yield pauses the fiber and returns control to the caller.
✗ Incorrect
The fiber prints "Step 1" then yields control. The main code prints "Between resumes" then resumes the fiber, which prints "Step 2".
🧠 Conceptual
intermediate1:30remaining
Fiber behavior on resume after completion
What happens if you call resume on a Ruby Fiber that has already finished executing?
Attempts:
2 left
💡 Hint
Think about whether fibers can be restarted once done.
✗ Incorrect
Calling resume on a finished fiber raises a FiberError because the fiber is dead and cannot be resumed.
🔧 Debug
advanced2:00remaining
Identify the error in Fiber usage
What error does this Ruby code produce when run?
Ruby
fiber = Fiber.new do Fiber.yield 1 Fiber.yield 2 end puts fiber.resume puts fiber.resume puts fiber.resume
Attempts:
2 left
💡 Hint
Check what happens when resume is called after the fiber finishes all yields.
✗ Incorrect
The third resume call tries to resume a dead fiber, causing FiberError.
🚀 Application
advanced2:30remaining
Using Fiber to alternate execution
Given two fibers that print numbers and letters alternately, what is the output of this code?
Ruby
numbers = Fiber.new do (1..3).each do |n| puts n Fiber.yield end end letters = Fiber.new do ['a', 'b', 'c'].each do |l| puts l Fiber.yield end end while numbers.alive? || letters.alive? numbers.resume if numbers.alive? letters.resume if letters.alive? end
Attempts:
2 left
💡 Hint
Fibers yield after printing each item, so they alternate execution.
✗ Incorrect
The numbers fiber prints 1, yields; letters fiber prints 'a', yields; and so on alternating.
🧠 Conceptual
expert2:00remaining
Fiber vs Thread in Ruby concurrency
Which statement best describes the difference between Ruby Fibers and Threads?
Attempts:
2 left
💡 Hint
Think about how control switches between fibers and threads.
✗ Incorrect
Fibers switch control only when they yield (cooperative), while threads can be interrupted by the scheduler (preemptive) and run in parallel.