0
0
Rubyprogramming~20 mins

Proc vs lambda differences (arity, return) in Ruby - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Proc vs Lambda Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output difference: Proc vs Lambda with arity mismatch
What is the output of this Ruby code?
Ruby
my_proc = Proc.new { |x| "Value: #{x}" }
my_lambda = ->(x) { "Value: #{x}" }

puts my_proc.call
puts my_lambda.call
A
Value: 
ArgumentError: wrong number of arguments (given 0, expected 1)
B
ArgumentError: wrong number of arguments (given 0, expected 1)
Value: 
C
Value: 
Value: 
D
ArgumentError: wrong number of arguments (given 0, expected 1)
ArgumentError: wrong number of arguments (given 0, expected 1)
Attempts:
2 left
💡 Hint
Consider how Proc and lambda handle missing arguments differently.
Predict Output
intermediate
2:00remaining
Return behavior difference between Proc and Lambda
What will be printed when running this Ruby code?
Ruby
def test_proc
  p = Proc.new { return "From Proc" }
  p.call
  return "From method"
end

def test_lambda
  l = -> { return "From Lambda" }
  l.call
  return "From method"
end

puts test_proc
puts test_lambda
A
From Proc
From Lambda
B
From method
From Lambda
C
From Proc
From method
D
From method
From method
Attempts:
2 left
💡 Hint
Think about how return works inside a Proc versus a lambda inside a method.
🔧 Debug
advanced
2:00remaining
Why does this lambda raise an error but the proc does not?
Consider this Ruby code snippet. Which option correctly explains why the lambda raises an error but the proc does not?
Ruby
my_proc = Proc.new { |a, b| a + (b || 0) }
my_lambda = ->(a, b) { a + b }

puts my_proc.call(5)
puts my_lambda.call(5)
AThe lambda enforces the number of arguments strictly, so calling with one argument raises ArgumentError; the proc allows missing arguments and assigns nil.
BThe proc enforces the number of arguments strictly, so calling with one argument raises ArgumentError; the lambda allows missing arguments and assigns nil.
CBoth proc and lambda allow missing arguments, but the lambda raises an error because of the addition operation.
DBoth proc and lambda raise ArgumentError because of missing arguments.
Attempts:
2 left
💡 Hint
Recall how procs and lambdas handle arity differently.
Predict Output
advanced
2:00remaining
Return inside Proc vs Lambda in nested method calls
What is the output of this Ruby code?
Ruby
def outer
  p = Proc.new { return "Proc return" }
  l = -> { return "Lambda return" }
  result_p = p.call
  result_l = l.call
  "Outer method end"
end

puts outer
ARuntimeError: unexpected return
BProc return
COuter method end
DLambda return
Attempts:
2 left
💡 Hint
Think about what happens when return is called inside a Proc inside a method.
🧠 Conceptual
expert
2:00remaining
Which statement about Proc and Lambda is true?
Select the only true statement about Ruby Proc and Lambda behavior.
ABoth Proc and Lambda enforce the exact number of arguments passed when called.
BA return inside a Proc exits only the Proc, while a return inside a Lambda exits the enclosing method.
CProcs and Lambdas behave identically in terms of arity and return behavior.
DA Lambda checks the number of arguments strictly and a return inside it exits only the Lambda, not the enclosing method.
Attempts:
2 left
💡 Hint
Recall the key differences in arity and return behavior between Proc and Lambda.