0
0
Rubyprogramming~10 mins

Ractor for true parallelism in Ruby - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Ractor for true parallelism
Create Ractor
Ractor runs code in parallel
Main thread continues
Send message to Ractor
Ractor processes message
Ractor sends result back
Main thread receives result
Ractor ends or waits for more messages
Ractor creates a parallel thread that runs code independently, communicates via messages, and allows true parallelism.
Execution Sample
Ruby
r = Ractor.new do
  msg = Ractor.receive
  msg * 2
end

r.send(10)
result = r.take
This code creates a Ractor that doubles a received number and sends back the result.
Execution Table
StepActionRactor StateMain Thread StateMessage PassingOutput
1Create RactorWaiting for messageRunning main codeNo message yetNone
2Main thread sends 10Received message 10Waiting for resultMessage 10 sent to RactorNone
3Ractor processes messageComputing 10 * 2 = 20Waiting for resultProcessing messageNone
4Ractor sends resultResult 20 readyWaiting to receiveResult 20 sent to main threadNone
5Main thread receives resultIdle or waitingReceived 20Result 20 received20
6Program ends or Ractor waitsIdle or terminatedProgram endsNo messages20
💡 Execution stops after main thread receives the doubled result and program ends.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 5Final
rRactor object createdRactor received 10Ractor computed 20Ractor idleRactor idle or terminated
msgNone1010NoneNone
resultNoneNoneNone2020
Key Moments - 3 Insights
Why does the main thread not wait immediately after creating the Ractor?
Because the Ractor runs in parallel, the main thread continues until it explicitly waits for a result (see step 2 and 5 in execution_table).
How does the main thread get the result from the Ractor?
The main thread uses r.take to receive the message sent back by the Ractor (step 5 in execution_table).
What happens if the Ractor does not receive a message?
The Ractor waits (blocks) at Ractor.receive until a message arrives (step 1 in execution_table).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the Ractor state right after the main thread sends 10?
AWaiting for message
BComputing 10 * 2 = 20
CReceived message 10
DIdle or terminated
💡 Hint
Check step 2 in the execution_table under 'Ractor State'.
At which step does the main thread receive the result from the Ractor?
AStep 5
BStep 4
CStep 3
DStep 6
💡 Hint
Look for 'Main thread receives result' in the execution_table.
If the main thread sends 20 instead of 10, what would change in the execution_table?
AThe Ractor would not receive any message
BThe Ractor state at step 3 would show computing 20 * 2 = 40
CThe main thread would receive 10 at step 5
DThe program would end earlier
💡 Hint
Check how the message value affects the computation in step 3.
Concept Snapshot
Ractor.new { code } creates a parallel Ractor.
Use r.send(msg) to send messages.
Use Ractor.receive inside Ractor to get messages.
Use r.take to get messages from Ractor.
Ractors run truly in parallel, no shared memory.
Communication only via messages.
Full Transcript
This example shows how Ruby's Ractor enables true parallelism by running code in a separate thread. The main thread creates a Ractor that waits for a message, doubles it, and sends back the result. The main thread sends the number 10, the Ractor receives it, computes 20, and sends it back. The main thread then receives 20 and continues. Variables like 'r', 'msg', and 'result' change states as messages pass. Key points include that the main thread does not block on Ractor creation, communication is via messages, and Ractor waits for messages if none are sent. The quiz tests understanding of Ractor states and message passing.