Procs and lambdas are ways to store blocks of code to run later. They look similar but behave differently in how they handle arguments and returns.
Proc vs lambda differences (arity, return) in Ruby
my_proc = Proc.new { |x| puts x } my_lambda = ->(x) { puts x }
Procs are created with Proc.new or proc.
Lambdas are created with lambda or the arrow syntax ->.
my_proc = Proc.new { |x, y| puts "x: #{x}, y: #{y}" } my_proc.call(1)
my_lambda = ->(x, y) { puts "x: #{x}, y: #{y}" }
my_lambda.call(1)def test_proc my_proc = Proc.new { return "From proc" } my_proc.call return "From method" end puts test_proc
def test_lambda my_lambda = -> { return "From lambda" } result = my_lambda.call return "From method after lambda" end puts test_lambda
This program shows how return works differently in proc and lambda. The proc's return exits the method immediately, so the lambda call is never reached.
def proc_vs_lambda my_proc = Proc.new { |x| return "Proc returned: #{x}" } my_lambda = ->(x) { return "Lambda returned: #{x}" } proc_result = my_proc.call(10) lambda_result = my_lambda.call(20) "proc_result: #{proc_result}, lambda_result: #{lambda_result}" end puts proc_vs_lambda
Procs are lenient with arguments; missing ones become nil.
Lambdas enforce the exact number of arguments.
Return inside a proc exits the surrounding method, but return inside a lambda exits only the lambda.
Procs are flexible with arguments and their return exits the whole method.
Lambdas check arguments strictly and their return exits only the lambda.
Choose based on how you want argument checking and return behavior.
Practice
Proc and a lambda in Ruby regarding argument checking?Solution
Step 1: Understand argument checking behavior
AProcin Ruby does not enforce the number of arguments strictly; it allows missing or extra arguments without error.Step 2: Compare with lambda behavior
Alambdabehaves like a method and enforces the exact number of arguments, raising an error if the count is wrong.Final Answer:
Procis lenient with arguments;lambdachecks arguments strictly. -> Option DQuick Check:
Argument checking = Proc lenient, lambda strict [OK]
- Thinking both check arguments strictly
- Confusing which one is strict
- Assuming Proc raises error on wrong args
Solution
Step 1: Identify lambda creation syntax
In Ruby, lambdas can be created using the literal syntax->(args) { block }orlambda { block }. The arrow syntax is common and concise.Step 2: Check each option
lambda = Proc.new { |x| x * 2 } creates a Proc, not a lambda. lambda = ->(x) { x * 2 } uses the correct lambda literal syntax. lambda = proc { |x| x * 2 } creates a Proc usingproc. lambda = lambda.new { |x| x * 2 } is invalid syntax.Final Answer:
lambda = ->(x) { x * 2 } -> Option CQuick Check:
Lambda syntax = ->(args) { block } [OK]
- Using Proc.new when lambda is needed
- Trying lambda.new which doesn't exist
- Confusing proc and lambda syntax
def test_proc
p = Proc.new { return "From Proc" }
p.call
return "From method"
end
puts test_procSolution
Step 1: Understand Proc return behavior
When aProcexecutes areturn, it returns from the enclosing method, not just the Proc block.Step 2: Trace the code execution
Callingp.calltriggers the Proc's return, which exits thetest_procmethod immediately with "From Proc". The laterreturn "From method"is never reached.Final Answer:
"From Proc" -> Option AQuick Check:
Proc return exits method = "From Proc" [OK]
- Thinking return exits only the Proc block
- Expecting "From method" output
- Confusing Proc and lambda return behavior
def test_lambda
l = lambda { |x| return x * 2 }
l.call
return "Done"
end
puts test_lambdaSolution
Step 1: Check lambda argument requirements
The lambda expects one argumentx, butl.callis called without arguments, causing an ArgumentError.Step 2: Fix the call by passing an argument
Callingl.call(5)or any value fixes the error. The lambda returnsx * 2, but this return exits only the lambda, so the method continues and returns "Done".Final Answer:
Error: lambda expects argument; fix by passing argument to l.call -> Option BQuick Check:
Lambda arity strict = pass correct args [OK]
- Ignoring missing argument error
- Thinking lambda return exits method
- Assuming no arguments needed for lambda
def test
l = ->(x) { return x * 2 }
p = Proc.new { return "Proc return" }
result_l = l.call(3)
result_p = p.call
return "Method end"
end
puts testSolution
Step 1: Execute lambda call
The lambdalis called with argument 3, returns 6, but itsreturnexits only the lambda, so method continues.Step 2: Execute Proc call
The Procpis called next. Itsreturnexits the entire method immediately with "Proc return". The laterreturn "Method end"is never reached.Final Answer:
"Proc return" -> Option AQuick Check:
Proc return exits method, lambda return exits lambda [OK]
- Expecting method to return "Method end"
- Confusing which return exits method
- Ignoring Proc's immediate method exit
Ready to prove it?
Pass the test and this pattern moves unknown
