Ruby Medium technical 0 views 1 min read

What is the difference between a block, a proc and a lambda?

Peer-reviewed by HireXTech Technical Panel Updated for 2025/2026 hiring Editorial standards
Practise this track
Interviewer Expectations for this Question
01
Core Competency

Assesses fundamental understanding of Ruby conventions, runtime behavior, and memory/performance considerations.

02
Evaluation Criteria

Hiring managers look for precision, avoidance of ambiguous jargon, and ability to explain trade-offs under real production conditions.

Comprehensive Model Answer Verified Solution

All three are chunks of code, but they differ in argument checking and how return behaves.

  • A block is passed to a method with do...end or braces and is not a standalone object unless converted with &.
  • A proc does not check argument count: missing arguments become nil and extras are dropped. A return returns from the enclosing method and can raise LocalJumpError if that method already returned.
  • A lambda checks arity strictly, and return returns 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

  1. Start with a concise one-sentence summary: Deliver a direct, confident answer first before expanding into nuances.
  2. Demonstrate real-world trade-offs: Discuss where this approach excels and when you would avoid it in production systems.
  3. Discuss complexity & edge cases: Proactively explain time/space complexity or boundary conditions (null values, scale limits).
  4. Prepare for interviewer follow-ups: Technical hiring panels frequently probe deeper into concurrency, backward compatibility, or alternative libraries.
Related Topics & Skills
Spotted an error or have an alternative solution?