0
0
Rubyprogramming~20 mins

Ractor for true parallelism in Ruby - Mini Project: Build & Apply

Choose your learning style9 modes available
Using Ractor for True Parallelism in Ruby
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use puts result1 + result2 to show the total sum.