A Proc is an object that holds a block of code which can be stored in a variable and called later. It allows you to treat code as data.
Click to reveal answer
beginner
How do you create a Proc in Ruby?
You can create a Proc using Proc.new { |args| ... } or the shorthand proc { |args| ... }.
Click to reveal answer
intermediate
What does Proc composition mean?
Proc composition means combining two or more Procs so that the output of one becomes the input of another, creating a chain of operations.
Click to reveal answer
intermediate
How can you compose two Procs in Ruby?
You can compose two Procs by creating a new Proc that calls the second Proc and then passes its result to the first Proc.
Click to reveal answer
intermediate
Example: Compose Proc p1 = proc { |x| x + 2 } and p2 = proc { |x| x * 3 }. What is the result of calling the composition with 4?
The composition applies p1 first: 4 + 2 = 6, then p2: 6 * 3 = 18. So the result is 18.
Click to reveal answer
What is the main purpose of a Proc in Ruby?
ATo create a variable
BTo store a block of code for later use
CTo define a class
DTo handle exceptions
✗ Incorrect
A Proc stores a block of code that can be called later.
How do you call a Proc stored in variable p?
Aexecute(p)
Bcall(p)
Cp()
Dp.call()
✗ Incorrect
You call a Proc by using the call method on it.
In Proc composition, what happens to the output of the first Proc?
AIt becomes input to the next Proc
BIt is ignored
CIt is printed automatically
DIt resets the program
✗ Incorrect
The output of the first Proc is passed as input to the next Proc in the composition.
Given p1 = proc { |x| x * 2 } and p2 = proc { |x| x + 3 }, what is the result of composing p2 after p1 and calling with 5?
A10
B16
C13
D8
✗ Incorrect
First p1: 5 * 2 = 10, then p2: 10 + 3 = 13.
Which Ruby method is used to create a Proc?
AProc.new
Bdef
Clambda
Dclass
✗ Incorrect
Proc.new creates a Proc object.
Explain what Proc composition is and how it works in Ruby.
Think about how you can connect small steps to make a bigger step.
You got /3 concepts.
Write a simple Ruby example that composes two Procs and explain the result.
Use simple math operations like addition and multiplication.
You got /3 concepts.
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]