📖 Scenario: You are working on a small Ruby program that processes numbers using reusable pieces of code called Procs. Procs are like little machines that take input and give output. You want to combine these machines to create a new machine that does two things in order.
🎯 Goal: Build a Ruby program that creates two simple Procs, then combines them into one Proc that applies both steps in sequence. Finally, run the combined Proc on a number and see the result.
📋 What You'll Learn
Create two Procs named double and increment.
double should multiply its input by 2.
increment should add 1 to its input.
Create a new Proc called double_then_increment that applies double first, then increment.
Call double_then_increment with the number 3 and print the result.
💡 Why This Matters
🌍 Real World
Proc composition is useful when you want to build complex operations from simple reusable steps, like processing data or applying filters.
💼 Career
Understanding Proc composition helps in writing clean, modular Ruby code often used in web development and scripting tasks.
Progress0 / 4 steps
1
Create two Procs
Create a Proc called double that multiplies its input by 2, and another Proc called increment that adds 1 to its input.
Ruby
Hint
Use Proc.new { |x| ... } to create each Proc.
2
Create a composed Proc
Create a new Proc called double_then_increment that takes one argument x, applies double to x, then applies increment to the result.
Ruby
Hint
Inside the new Proc, call double.call(x) first, then pass that result to increment.call(...).
3
Call the composed Proc
Call the Proc double_then_increment with the number 3 and store the result in a variable called result.
Ruby
Hint
Use double_then_increment.call(3) and assign it to result.
4
Print the result
Print the value of the variable result.
Ruby
Hint
Use puts result to display the output.
Practice
(1/5)
1. What does the >> operator do when used with two Procs in Ruby?
easy
A. It merges two Procs into one that runs them both in parallel.
B. It creates a new Proc that runs the first Proc, then passes its result to the second Proc.
C. It swaps the order of execution of two Procs.
D. It deletes the second Proc and keeps only the first.
Solution
Step 1: Understand Proc composition operator
The >> operator chains two Procs so the output of the first becomes input of the second.
Step 2: Explain the effect of chaining
Calling the combined Proc runs the first, then passes its result to the second, producing a final result.
Final Answer:
It creates a new Proc that runs the first Proc, then passes its result to the second Proc. -> Option B
Quick Check:
Proc composition = chaining with >> [OK]
Hint: Remember >> chains Procs passing output as input [OK]
Common Mistakes:
Thinking >> runs Procs in parallel
Assuming >> swaps execution order
Believing >> deletes one Proc
2. Which of the following is the correct syntax to compose two Procs p1 and p2 in Ruby?
easy
A. combined = p1 >> p2
B. combined = p1 >>> p2
C. combined = p1 << p2
D. combined = p1 >> p2.call
Solution
Step 1: Recall correct Proc composition operator
The operator to compose Procs is >>, which is written as >> but in Ruby code it is >> (two greater-than signs).
Step 2: Check each option
combined = p1 >> p2 uses >> correctly. combined = p1 >>> p2 uses three > which is invalid. combined = p1 << p2 uses << which is not for composition. combined = p1 >> p2.call calls p2 immediately, which is wrong.
Final Answer:
combined = p1 >> p2 -> Option A
Quick Check:
Proc composition syntax = p1 >> p2 [OK]
Hint: Use exactly two > signs to compose Procs [OK]