📖 Scenario: You want to speed up a task by running parts of it at the same time using Ruby's Ractor feature. This helps use multiple CPU cores to do work faster.
🎯 Goal: Build a Ruby program that uses Ractor to run two tasks in parallel and then combines their results.
📋 What You'll Learn
Create two Ractors that each calculate a sum of numbers
Use a variable to hold the range limit for the sums
Collect results from both Ractors
Print the combined total sum
💡 Why This Matters
🌍 Real World
Using Ractors helps programs run faster by doing multiple things at once, like processing big data or handling many users.
💼 Career
Understanding Ractors is useful for Ruby developers working on performance-critical applications or server-side programs that need true parallelism.
Progress0 / 4 steps
1
Create a variable with the range limit
Create a variable called limit and set it to 1000000.
Ruby
Hint
Use = to assign the number 1000000 to the variable limit.
2
Create two Ractors to calculate sums in parallel
Create two Ractors named r1 and r2. Each should calculate the sum of numbers from 1 to limit divided by 2. Use Ractor.new and a block that sums the range.
Ruby
Hint
Use Ractor.new(limit) { |n| ... } to create each Ractor. Use sum on the range inside the block.
3
Receive results from both Ractors
Create two variables result1 and result2. Assign them the values received from r1.take and r2.take respectively.
Ruby
Hint
Use r1.take and r2.take to get the results from each Ractor.
4
Print the combined total sum
Print the sum of result1 and result2 using puts.
Ruby
Hint
Use puts result1 + result2 to show the total sum.
Practice
(1/5)
1. What is the main purpose of using Ractor in Ruby?
easy
A. To run code truly in parallel on multiple CPU cores
B. To create graphical user interfaces
C. To handle database connections
D. To write single-threaded scripts
Solution
Step 1: Understand Ractor's role
Ractor is designed to enable true parallel execution in Ruby, using multiple CPU cores.
Step 2: Compare options
Options A, B, and C do not relate to parallelism or CPU core usage.
Final Answer:
To run code truly in parallel on multiple CPU cores -> Option A
Quick Check:
Ractor = true parallelism [OK]
Hint: Ractor means real parallel tasks in Ruby [OK]
Common Mistakes:
Confusing Ractor with threads
Thinking Ractor is for UI or database
Assuming Ractor runs code sequentially
2. Which of the following is the correct way to create a new Ractor in Ruby?
easy
A. Ractor.new { puts 'Hello from Ractor' }
B. Thread.new { puts 'Hello from Ractor' }
C. Process.spawn { puts 'Hello from Ractor' }
D. Fiber.new { puts 'Hello from Ractor' }
Solution
Step 1: Identify Ractor creation syntax
Ractor is created using Ractor.new with a block of code to run in parallel.
Step 2: Eliminate other options
Thread, Process, and Fiber are different concurrency tools, not Ractor.
Final Answer:
Ractor.new { puts 'Hello from Ractor' } -> Option A
Quick Check:
Use Ractor.new to create Ractors [OK]
Hint: Ractor.new starts a parallel task [OK]
Common Mistakes:
Using Thread.new instead of Ractor.new
Confusing Process.spawn with Ractor
Using Fiber.new which is cooperative, not parallel
3. What will be the output of this Ruby code using Ractor?
r = Ractor.new { 5 * 5 }
result = r.take
puts result
medium
A. RactorError
B. 5 * 5
C. nil
D. 25
Solution
Step 1: Understand Ractor block execution
The block { 5 * 5 } runs inside the Ractor and returns 25.
Step 2: Use take to get the result
r.take receives the value returned by the Ractor block, which is 25.
Final Answer:
25 -> Option D
Quick Check:
Ractor returns 25, take receives it [OK]
Hint: Ractor block returns value, take fetches it [OK]
Common Mistakes:
Expecting string '5 * 5' instead of calculation
Confusing take with yield
Assuming nil or error without communication
4. Identify the error in this Ractor code snippet:
r = Ractor.new do
Ractor.yield 10
20
end
puts r.take
puts r.take
medium
A. Ractor.new requires arguments
B. Second r.take call will cause Ractor::Closed error
C. No error, code runs fine
D. Calling Ractor.yield inside Ractor block is invalid
Solution
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.
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.
Step 3: Check the code carefully
Both r.take calls are valid; no error occurs.
Final Answer:
No error, code runs fine -> Option C
Quick Check:
Ractor.yield and take work twice safely [OK]
Hint: 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
5. You want to sum numbers from 1 to 100 using two Ractors running in parallel, each summing half the range. Which code correctly implements this?