Bird
Raised Fist0
Rubyprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. Which statement correctly describes the difference between a Proc and a lambda in Ruby regarding argument checking?
easy
A. Both Proc and lambda check arguments strictly.
B. Proc checks arguments strictly; lambda is lenient.
C. Neither Proc nor lambda check arguments.
D. Proc is lenient with arguments; lambda checks arguments strictly.

Solution

  1. Step 1: Understand argument checking behavior

    A Proc in Ruby does not enforce the number of arguments strictly; it allows missing or extra arguments without error.
  2. Step 2: Compare with lambda behavior

    A lambda behaves like a method and enforces the exact number of arguments, raising an error if the count is wrong.
  3. Final Answer:

    Proc is lenient with arguments; lambda checks arguments strictly. -> Option D
  4. Quick Check:

    Argument checking = Proc lenient, lambda strict [OK]
Hint: Remember: lambda acts like a method, Proc is more flexible [OK]
Common Mistakes:
  • Thinking both check arguments strictly
  • Confusing which one is strict
  • Assuming Proc raises error on wrong args
2. Which of the following is the correct syntax to create a lambda in Ruby?
easy
A. lambda = Proc.new { |x| x * 2 }
B. lambda = proc { |x| x * 2 }
C. lambda = ->(x) { x * 2 }
D. lambda = lambda.new { |x| x * 2 }

Solution

  1. Step 1: Identify lambda creation syntax

    In Ruby, lambdas can be created using the literal syntax ->(args) { block } or lambda { block }. The arrow syntax is common and concise.
  2. 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 using proc. lambda = lambda.new { |x| x * 2 } is invalid syntax.
  3. Final Answer:

    lambda = ->(x) { x * 2 } -> Option C
  4. Quick Check:

    Lambda syntax = ->(args) { block } [OK]
Hint: Use ->(args) { } for lambda, Proc.new or proc for Proc [OK]
Common Mistakes:
  • Using Proc.new when lambda is needed
  • Trying lambda.new which doesn't exist
  • Confusing proc and lambda syntax
3. What will be the output of the following Ruby code?
def test_proc
  p = Proc.new { return "From Proc" }
  p.call
  return "From method"
end

puts test_proc
medium
A. "From Proc"
B. "From method"
C. nil
D. RuntimeError

Solution

  1. Step 1: Understand Proc return behavior

    When a Proc executes a return, it returns from the enclosing method, not just the Proc block.
  2. Step 2: Trace the code execution

    Calling p.call triggers the Proc's return, which exits the test_proc method immediately with "From Proc". The later return "From method" is never reached.
  3. Final Answer:

    "From Proc" -> Option A
  4. Quick Check:

    Proc return exits method = "From Proc" [OK]
Hint: Proc return exits method; lambda return exits lambda only [OK]
Common Mistakes:
  • Thinking return exits only the Proc block
  • Expecting "From method" output
  • Confusing Proc and lambda return behavior
4. Identify the error in the following Ruby code and fix it:
def test_lambda
  l = lambda { |x| return x * 2 }
  l.call
  return "Done"
end

puts test_lambda
medium
A. No error; output is "Done"
B. Error: lambda expects argument; fix by passing argument to l.call
C. SyntaxError: lambda block missing do/end
D. Error: lambda return exits method; fix by removing return inside lambda

Solution

  1. Step 1: Check lambda argument requirements

    The lambda expects one argument x, but l.call is called without arguments, causing an ArgumentError.
  2. Step 2: Fix the call by passing an argument

    Calling l.call(5) or any value fixes the error. The lambda returns x * 2, but this return exits only the lambda, so the method continues and returns "Done".
  3. Final Answer:

    Error: lambda expects argument; fix by passing argument to l.call -> Option B
  4. Quick Check:

    Lambda arity strict = pass correct args [OK]
Hint: Lambda needs exact args; always pass them when calling [OK]
Common Mistakes:
  • Ignoring missing argument error
  • Thinking lambda return exits method
  • Assuming no arguments needed for lambda
5. Given the following code, what will be the output?
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 test
hard
A. "Proc return"
B. "Method end"
C. 6
D. ArgumentError

Solution

  1. Step 1: Execute lambda call

    The lambda l is called with argument 3, returns 6, but its return exits only the lambda, so method continues.
  2. Step 2: Execute Proc call

    The Proc p is called next. Its return exits the entire method immediately with "Proc return". The later return "Method end" is never reached.
  3. Final Answer:

    "Proc return" -> Option A
  4. Quick Check:

    Proc return exits method, lambda return exits lambda [OK]
Hint: Proc return exits method immediately; lambda return does not [OK]
Common Mistakes:
  • Expecting method to return "Method end"
  • Confusing which return exits method
  • Ignoring Proc's immediate method exit
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard