A Proc is an object that holds a block of code which can be stored in a variable and called later.
Click to reveal answer
beginner
How do you create a Proc in Ruby?
You create a Proc by using Proc.new { ... } or the shorthand proc { ... }.
Click to reveal answer
beginner
How do you call a Proc once it is created?
You call a Proc using proc_object.call(arguments).
Click to reveal answer
intermediate
What happens if you call a Proc with the wrong number of arguments?
A Proc is flexible and does not raise an error if called with fewer or more arguments than expected. Missing arguments become nil.
Click to reveal answer
intermediate
Difference between Proc and lambda in Ruby?
A Proc is more lenient with arguments and returns from the calling method, while a lambda checks arguments strictly and returns from itself.
Click to reveal answer
How do you create a Proc object in Ruby?
Aproc = function() { puts 'Hello' }
Bdef proc { puts 'Hello' }
Ccreate Proc { puts 'Hello' }
DProc.new { puts 'Hello' }
✗ Incorrect
You create a Proc using Proc.new with a block, like Proc.new { puts 'Hello' }.
How do you execute the code inside a Proc named 'p'?
Ap.call()
Bexecute p()
Ccall p()
Dp.run()
✗ Incorrect
You call a Proc using the call method: p.call().
What happens if you call a Proc with fewer arguments than it expects?
AMissing arguments become nil
BIt raises an error
CIt ignores extra arguments
DIt converts arguments to strings
✗ Incorrect
A Proc assigns nil to missing arguments instead of raising an error.
Which of these is true about Procs compared to lambdas?
AProcs check argument count strictly
BProcs return from the calling method
CProcs cannot be stored in variables
DProcs are not objects
✗ Incorrect
Procs return from the calling method, unlike lambdas which return from themselves.
Which syntax creates a Proc using shorthand in Ruby?
Alambda { puts 'Hi' }
BProc.new do puts 'Hi' end
Cproc { puts 'Hi' }
Ddef proc; puts 'Hi'; end
✗ Incorrect
The shorthand to create a Proc is using proc { ... }.
Explain how to create and call a Proc in Ruby with an example.
Think about how you store a block of code and run it later.
You got /3 concepts.
Describe the difference between a Proc and a lambda in Ruby.
Focus on how they treat arguments and return statements.
You got /3 concepts.
Practice
(1/5)
1. What does a Proc in Ruby do?
easy
A. Stores a block of code to run later
B. Creates a new variable
C. Defines a class
D. Starts a loop
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 A
Quick Check:
Proc = stored code block [OK]
Hint: Proc saves code to run later, like a saved recipe [OK]
Common Mistakes:
Thinking Proc creates variables
Confusing Proc with class definition
Assuming Proc starts loops automatically
2. Which of the following is the correct way to create a Proc in Ruby?
easy
A. my_proc = Proc.new { |x| x * 2 }
B. my_proc = new Proc { |x| x * 2 }
C. my_proc = Proc.create { |x| x * 2 }
D. my_proc = Proc.call { |x| x * 2 }
Solution
Step 1: Recall Proc creation syntax and check options
In Ruby, you create a Proc using Proc.new or proc with a block. Only my_proc = Proc.new { |x| x * 2 } uses the correct syntax Proc.new { |x| x * 2 }. Others use invalid methods like new Proc, Proc.create, or Proc.call.
Final Answer:
my_proc = Proc.new { |x| x * 2 } -> Option A
Quick Check:
Proc.new + block = correct Proc creation [OK]
Hint: Use Proc.new or proc with a block to create a Proc [OK]
Common Mistakes:
Using 'new Proc' instead of 'Proc.new'
Trying to call Proc.create or Proc.call to create
Missing the block braces {}
3. What is the output of this Ruby code?
my_proc = proc { |x| x + 3 }
puts my_proc.call(4)
medium
A. 4
B. Error
C. 3
D. 7
Solution
Step 1: Trace the Proc definition and execution
The Proc adds 3 to the input number: x + 3. Calling my_proc.call(4) computes 4 + 3 = 7.
Final Answer:
7 -> Option D
Quick Check:
4 + 3 = 7 [OK]
Hint: Proc.call runs code with given argument [OK]