Ractors
True parallelism.
Ractors is a free Ruby Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is a Ractor?
A Ractor (Ruby Actor) is a concurrency primitive introduced in Ruby 3 that provides true parallelism. Each Ractor runs Ruby code on its own, so two Ractors can use two CPU cores at the same time.
- Ractors do not share mutable objects, which sidesteps the GVL limitation.
- They communicate by sending messages.
Ractors are still experimental, so Ruby prints a warning when you use them.
Creating a Ractor
Create one with Ractor.new and a block. The block runs in parallel. Use take to get the block's final value.
r = Ractor.new do
2 + 3
end
puts r.takeNo Shared State
The big rule: Ractors cannot freely share mutable objects. A Ractor block does not capture outer local variables the way a normal block does. You must pass data in explicitly.
This isolation is what makes parallel execution safe without locks.
Passing Arguments In
Arguments to Ractor.new are delivered to the block parameters. This is the safe way to send data into a Ractor.
r = Ractor.new(10) do |x|
x * x
end
puts r.takeSending Messages
Ractors have a message inbox. Send with ractor.send(value) and receive inside the block with Ractor.receive.
r = Ractor.new do
msg = Ractor.receive
"got: " + msg
end
r.send("hello")
puts r.takePush vs Pull
Two communication directions:
- Push:
sendinto a Ractor, read withRactor.receive. - Pull: a Ractor yields with
Ractor.yield, the outside reads withtake.
Together they let Ractors form pipelines.
r = Ractor.new do
Ractor.yield 1
Ractor.yield 2
end
puts r.take
puts r.takeFrozen and Shareable Objects
Some objects are shareable across Ractors because they cannot be mutated: true, false, nil, numbers, symbols, and deeply frozen objects.
Ractor.make_shareable freezes an object graph so it can be shared safely.
data = Ractor.make_shareable([1, 2, 3])
puts "Frozen? " + data.frozen?.to_sCopy vs Move
When you send a non-shareable object, Ractor deep-copies it by default so each side has its own. You can instead move it with send(obj, move: true), which transfers ownership and makes the original unusable.
Move avoids copy cost for large objects.
Parallel Work
Split CPU-heavy work across Ractors and collect results. Because each runs on its own, this can use multiple cores.
ractors = (1..3).map do |n|
Ractor.new(n) do |x|
x * 100
end
end
results = ractors.map(&:take)
puts "Results: " + results.join(", ")Ractors vs Threads
Comparison:
- Threads share memory and need locks; Ractors are isolated and need messages.
- Threads do not parallelize Ruby CPU work; Ractors do.
- Ractors are stricter, which prevents many concurrency bugs.
The tradeoff is more rigid data sharing rules.
When to Use Ractors
Choose Ractors when:
- You have CPU-bound work and want multiple cores in one process.
- You want isolation to avoid shared-state bugs.
Remember they are experimental; APIs may change, and many gems are not Ractor-safe yet.
Quick Check
Test your understanding of Ractors.
Recap: Ractors
You learned in-process parallelism with Ractors:
Ractor.newruns code in parallel;takegets its result.- Pass data in via arguments or
send; receive withRactor.receive. - Objects are copied or moved, not freely shared.
Ractor.make_shareablefreezes data for safe sharing.
That completes the Ruby Concurrency course: threads, the GVL, fibers, and Ractors.
Frequently asked questions
Is the “Ractors” lesson free?
Yes — the full text of “Ractors” is free to read here on the web, and the Ruby Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ruby Academy course, upgrade to CoddyKit PRO.
What will I learn in “Ractors”?
True parallelism. 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; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Ractors” 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.