📖 Scenario: Imagine you run a small online store. You want your website to handle many customers at the same time without slowing down. This is where concurrency helps your Ruby program do many things at once smoothly.
🎯 Goal: You will create a simple Ruby program that shows how concurrency lets your program do multiple tasks at the same time. This helps your store serve many customers quickly.
📋 What You'll Learn
Create a list of tasks representing customer orders
Set a limit for how many tasks can run at the same time
Use Ruby threads to process tasks concurrently
Print messages showing when each task starts and finishes
💡 Why This Matters
🌍 Real World
Concurrency helps websites and apps handle many users or tasks at once without slowing down.
💼 Career
Understanding concurrency is important for Ruby developers building fast, responsive applications.
Progress0 / 4 steps
1
Create a list of customer orders
Create an array called orders with these exact strings: 'Order 1', 'Order 2', 'Order 3', 'Order 4', 'Order 5'.
Ruby
Hint
Use square brackets [] to create an array and put the order names inside quotes separated by commas.
2
Set the maximum number of concurrent tasks
Create a variable called max_threads and set it to 2 to limit how many orders can be processed at the same time.
Ruby
Hint
Just write max_threads = 2 on a new line.
3
Process orders concurrently using threads
Use a threads array to hold threads. Use a for loop with variable order to go through orders. Inside the loop, create a new thread that prints "Starting #{order}", sleeps for 1 second, then prints "Finished #{order}". Add the thread to threads. If the number of threads reaches max_threads, wait for the first thread to finish before continuing.
Ruby
Hint
Use Thread.new to create a thread. Use threads.shift.join to wait for the oldest thread when max_threads is reached.
4
Print the final output showing concurrency
Run the program to print messages showing when each order starts and finishes. The output should show that up to 2 orders run at the same time.
Ruby
Hint
Run the program and watch the messages. You should see two orders starting before any finish, showing concurrency.
Practice
(1/5)
1. Why is concurrency important in Ruby programs?
easy
A. It allows the program to do multiple tasks at the same time, improving speed.
B. It makes the program use less memory.
C. It automatically fixes bugs in the code.
D. It changes the program's output format.
Solution
Step 1: Understand concurrency's purpose
Concurrency lets a program handle multiple tasks simultaneously instead of one after another.
Step 2: Identify the benefit in Ruby
This improves speed and responsiveness, especially when waiting for slow tasks or handling many users.
Final Answer:
It allows the program to do multiple tasks at the same time, improving speed. -> Option A
Quick Check:
Concurrency = multitasking speedup [OK]
Hint: Concurrency means doing many things at once [OK]
Common Mistakes:
Thinking concurrency reduces memory usage
Believing concurrency fixes bugs automatically
Confusing concurrency with output formatting
2. Which of the following is the correct way to start a new thread in Ruby?
easy
A. Thread.new { puts 'Hello from thread' }
B. thread.start { puts 'Hello from thread' }
C. new Thread { puts 'Hello from thread' }
D. Thread.run { puts 'Hello from thread' }
Solution
Step 1: Recall Ruby thread syntax
In Ruby, threads are created using Thread.new with a block of code.
Step 2: Check each option
Only Thread.new { ... } is valid Ruby syntax to start a thread.
Final Answer:
Thread.new { puts 'Hello from thread' } -> Option A
Quick Check:
Thread.new starts threads [OK]
Hint: Use Thread.new with a block to start threads [OK]