What is the difference between a block, a proc and a lambda?
Assesses fundamental understanding of Ruby conventions, runtime behavior, and memory/performance considerations.
Hiring managers look for precision, avoidance of ambiguous jargon, and ability to explain trade-offs under real production conditions.
All three are chunks of code, but they differ in argument checking and how return behaves.
- A
blockis passed to a method withdo...endor braces and is not a standalone object unless converted with&. - A
procdoes not check argument count: missing arguments become nil and extras are dropped. Areturnreturns from the enclosing method and can raiseLocalJumpErrorif that method already returned. - A
lambdachecks arity strictly, andreturnreturns only from the lambda.
def run(callable)
callable.call
:after
end
l = -> { return :from_lambda }
puts run(l) # :after
pr = proc { return :from_proc }
# run(pr) raises LocalJumpError
Lambdas are usually preferred because they behave like methods. Check with lambda?. Blocks are typically yielded once and converted to a Proc through the &block parameter.
Candidate Response Strategy & Interview Tips
- Start with a concise one-sentence summary: Deliver a direct, confident answer first before expanding into nuances.
- Demonstrate real-world trade-offs: Discuss where this approach excels and when you would avoid it in production systems.
- Discuss complexity & edge cases: Proactively explain time/space complexity or boundary conditions (null values, scale limits).
- Prepare for interviewer follow-ups: Technical hiring panels frequently probe deeper into concurrency, backward compatibility, or alternative libraries.