0
0
Rubyprogramming~5 mins

Proc vs lambda differences (arity, return) in Ruby - Quick Revision & Key Differences

Choose your learning style9 modes available
Recall & Review
beginner
What is a Proc in Ruby?
A Proc is a block of code that can be stored in a variable and called later. It behaves like a method but is more flexible and can be passed around.
Click to reveal answer
intermediate
How does a lambda handle the number of arguments (arity) compared to a Proc?
A lambda checks the number of arguments strictly and raises an error if the count doesn't match. A Proc is more lenient and ignores extra arguments or assigns nil to missing ones.
Click to reveal answer
intermediate
What happens when you use return inside a lambda vs inside a Proc?
Inside a lambda, return exits only from the lambda itself. Inside a Proc, return exits from the method enclosing the Proc, which can cause unexpected behavior.
Click to reveal answer
beginner
Which behaves more like a method: Proc or lambda?
A lambda behaves more like a method because it checks arguments strictly and its return exits only from itself, not the enclosing method.
Click to reveal answer
beginner
How do you create a lambda and a Proc in Ruby?
You create a lambda with lambda { |args| ... } or ->(args) { ... }. You create a Proc with Proc.new { |args| ... } or proc { |args| ... }.
Click to reveal answer
What happens if you pass the wrong number of arguments to a lambda?
AIt assigns nil to missing arguments
BIt ignores extra arguments
CIt runs without any checks
DIt raises an error
What does return inside a Proc do?
AExits the enclosing method
BExits only the Proc
CRaises an error
DDoes nothing
Which one behaves more like a method in Ruby?
Alambda
BNeither
CBoth behave the same
DProc
How do you create a lambda in Ruby?
AProc.new { ... }
Blambda { ... }
Cdef lambda ... end
Dcreate_lambda { ... }
If a Proc receives fewer arguments than expected, what happens?
AIt ignores missing arguments
BIt raises an error
CIt assigns nil to missing arguments
DIt stops execution
Explain the differences between Proc and lambda in Ruby regarding argument handling and return behavior.
Think about how each handles wrong number of arguments and where return goes.
You got /3 concepts.
    Describe how you create and use a Proc and a lambda in Ruby with examples.
    Show simple code snippets and explain behavior.
    You got /4 concepts.