Bird
0
0

Identify the error in this Ractor code snippet:

medium📝 Debug Q14 of 15
Ruby - Concurrent Programming
Identify the error in this Ractor code snippet:
r = Ractor.new do
  Ractor.yield 10
  20
end
puts r.take
puts r.take
ARactor.new requires arguments
BSecond r.take call will cause Ractor::Closed error
CNo error, code runs fine
DCalling Ractor.yield inside Ractor block is invalid
Step-by-Step Solution
Solution:
  1. Step 1: Understand Ractor.yield and r.take

    Ractor.yield sends a value to the main thread, and r.take receives it. The first r.take gets 10.
  2. Step 2: Analyze second r.take call

    The block returns 20 after yield, which is the final value. The second r.take gets 20. After that, the Ractor is closed. A third r.take would cause error, but here only two calls are made, so no error.
  3. Step 3: Check the code carefully

    Both r.take calls are valid; no error occurs.
  4. Final Answer:

    No error, code runs fine -> Option C
  5. Quick Check:

    Ractor.yield and take work twice safely [OK]
Quick Trick: Ractor.yield sends values, take receives them sequentially [OK]
Common Mistakes:
  • Thinking Ractor.yield is invalid inside Ractor
  • Expecting error on second take call
  • Assuming Ractor.new needs arguments

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes