Bird
0
0

What will be the output of the following Ruby code?

medium📝 Predict Output Q13 of 15
Ruby - Concurrent Programming
What will be the output of the following Ruby code?
fiber = Fiber.new do
  puts 'Step 1'
  Fiber.yield
  puts 'Step 2'
end

fiber.resume
puts 'Between'
fiber.resume
AStep 2 Between Step 1
BStep 1 Step 2 Between
CBetween Step 1 Step 2
DStep 1 Between Step 2
Step-by-Step Solution
Solution:
  1. Step 1: Trace first resume call

    Calling fiber.resume runs until Fiber.yield, printing 'Step 1'.
  2. Step 2: Trace code after first resume

    After yield, control returns to main, printing 'Between'.
  3. Step 3: Trace second resume call

    Second fiber.resume resumes after yield, printing 'Step 2'.
  4. Final Answer:

    Step 1 Between Step 2 -> Option D
  5. Quick Check:

    Fiber.yield pauses, resume continues [OK]
Quick Trick: Fiber.yield pauses; resume continues after yield [OK]
Common Mistakes:
  • Assuming all code runs at first resume
  • Confusing order of prints around yield
  • Thinking yield stops Fiber permanently

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes