midRuby

What is the difference between blocks, procs, and lambdas in Ruby?

Updated May 17, 2026

Short answer

Blocks are syntactic chunks of code passed to methods. Procs are objects enclosing blocks that exhibit lenient argument checking and return from the calling method context. Lambdas are strict Procs that validate argument counts and return locally.

Deep explanation

Blocks are not objects; they are syntactic constructs parsed at invocation time via yield or &block. Procs (Proc.new) are full closures instantiated into objects. They do not enforce strict parameter counts (missing args become nil). A return statement inside a Proc exits the enclosing method. Lambdas (lambda or ->{}) are a specific sub-type of Proc. They behave like normal methods: they raise an ArgumentError if parameter counts mismatch, and a return statement exits only the lambda code block itself.

Real-world example

Lambdas are used for reusable, isolated calculations or data transformations (like Rails scopes). Procs are useful when creating early exit flows or DSL blocks that need to short-circuit a higher-level processing pipeline.

Common mistakes

  • Using `return` inside a Proc passed to an iterator block, accidentally exiting the entire surrounding method rather than skipping to the next element iteration.

Follow-up questions

  • How do you check if a Proc object is a lambda?
  • What happens if you pass too many arguments to a lambda?

More Ruby interview questions

View all →