Proc creation and call in Ruby - Time & Space Complexity
We want to understand how the time it takes to create and call a Proc changes as we do it more times.
How does the number of Proc creations and calls affect the total work done?
Analyze the time complexity of the following code snippet.
procs = []
10.times do |i|
procs << Proc.new { i * 2 }
end
procs.each do |p|
p.call
end
This code creates 10 Procs that double a number, then calls each Proc once.
Look at what repeats in the code.
- Primary operation: Creating Procs inside a loop and calling each Proc.
- How many times: Each happens 10 times, once per item.
As the number of Procs grows, the work grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 20 (10 creations + 10 calls) |
| 100 | About 200 (100 creations + 100 calls) |
| 1000 | About 2000 (1000 creations + 1000 calls) |
Pattern observation: The total work grows directly with the number of Procs created and called.
Time Complexity: O(n)
This means if you double the number of Procs, the total time roughly doubles too.
[X] Wrong: "Creating a Proc once means calling it many times is free or very fast."
[OK] Correct: Each call still takes time, so calling many times adds up and affects total time.
Understanding how repeated creation and calls affect time helps you reason about code efficiency and resource use in real projects.
"What if we created one Proc and called it multiple times instead of creating many Procs? How would the time complexity change?"
Practice
Proc in Ruby do?Solution
Step 1: Understand what a Proc is and match description
A Proc is a way to save some code to run later, like a recipe you keep for cooking. Only Stores a block of code to run later describes storing code to run later, which matches what a Proc does.Final Answer:
Stores a block of code to run later -> Option AQuick Check:
Proc = stored code block [OK]
- Thinking Proc creates variables
- Confusing Proc with class definition
- Assuming Proc starts loops automatically
Solution
Step 1: Recall Proc creation syntax and check options
In Ruby, you create a Proc usingProc.neworprocwith a block. Only my_proc = Proc.new { |x| x * 2 } uses the correct syntaxProc.new { |x| x * 2 }. Others use invalid methods likenew Proc,Proc.create, orProc.call.Final Answer:
my_proc = Proc.new { |x| x * 2 } -> Option AQuick Check:
Proc.new + block = correct Proc creation [OK]
- Using 'new Proc' instead of 'Proc.new'
- Trying to call Proc.create or Proc.call to create
- Missing the block braces {}
my_proc = proc { |x| x + 3 }
puts my_proc.call(4)Solution
Step 1: Trace the Proc definition and execution
The Proc adds 3 to the input number:x + 3. Callingmy_proc.call(4)computes 4 + 3 = 7.Final Answer:
7 -> Option DQuick Check:
4 + 3 = 7 [OK]
- Confusing Proc.call with Proc.new
- Forgetting to pass argument to call
- Expecting output without call
my_proc = Proc.new { |x| x * 2 }
puts my_proc.callSolution
Step 1: Analyze Proc definition and call for errors
The Proc expects one argumentxto multiply by 2. The call is made without any argument, which causes an error becausexis missing.Final Answer:
Missing argument when calling the Proc -> Option BQuick Check:
Proc needs argument when called [OK]
- Calling Proc without required arguments
- Thinking Proc.new syntax is wrong
- Believing call method is invalid
def create_proc
Proc.new { |x| x.nil? ? 'No value' : x * 10 }
end
my_proc = create_proc
puts my_proc.call(nil)
puts my_proc.call(5)Solution
Step 1: Analyze the Proc logic and trace both calls
The Proc checks ifxis nil; if yes, returns 'No value', else returnsx * 10. Calling withnilreturns 'No value'. Calling with 5 returns 5 * 10 = 50.Final Answer:
No value\n50 -> Option CQuick Check:
nil -> 'No value', 5 -> 50 [OK]
- Expecting error when passing nil
- Forgetting ternary condition inside Proc
- Assuming multiplication by 10 applies to nil
Ready to prove it?
Pass the test and this pattern moves unknown
