Skip to content
Poignant Guide Ruby Q&A

What is the difference between a proc and a lambda in Ruby?

Staff answerWritten by Odile Marchetti · Blocks, procs and iterators0 replies

Short answer

Both are objects that hold code you can call later. They differ in two ways: a lambda checks its argument count like a method does, and return inside a lambda leaves only the lambda, while return inside a proc leaves the method around it. Reach for a lambda by default, and a proc for a block's looser rules.

Written by Odile Marchetti · Staff: Ruby basics, objects and the Poignant Guide ·
CitePoignant Guide Ruby Q&A, "What is the difference between a proc and a lambda in Ruby?", https://poignantguide.net/answers/read/what-is-the-difference-between-a-proc-and-a-lambda-in-ruby/, accessed 2026-11-21.
  1. Staff: Ruby basics, objects and the Poignant Guide ·

    Side by side in irb

    A proc is a block saved in a variable so it can be passed around and called later. A lambda is a proc with stricter, method-like rules. Paste this:

    sq = proc   { |x, y| "x=#{x}, y=#{y}" }
    lm = lambda { |x, y| "x=#{x}, y=#{y}" }
    
    p sq.call(1)
    # => "x=1, y="
    p sq.call(1, 2, 3)
    # => "x=1, y=2"
    p sq.call([1, 2])
    # => "x=1, y=2"
    
    lm.call(1)
    # ArgumentError: wrong number of arguments (given 1, expected 2)
    
    p sq.lambda?, lm.lambda?
    # => false
    # => true
    

    Those results match the Proc documentation, which says regular procs fill in missing arguments with nil, take apart a single array argument if the proc has several parameters, and raise no error on extra arguments, while lambdas treat arguments "in the same way as in methods: strict, with ArgumentError for mismatching argument number".

    The two differences that matter

    1. Argument handling. A lambda is picky and a proc is relaxed, as above. A picky tool tells you early when you passed the wrong number of things, which is why lambdas suit small self-contained functions.

    2. What return means. This is the one that surprises people. Run both:

    def run_proc
      pr = Proc.new { return 10 }
      pr.call
      20
    end
    
    def run_lambda
      l = -> { return 10 }
      l.call
      20
    end
    
    p run_proc
    # => 10
    p run_lambda
    # => 20
    

    In the proc version, return exits the whole run_proc method, so line 20 never runs. In the lambda version, return only exits the lambda, and the method carries on to 20. The documentation puts it this way: in lambdas "return and break means exit from this lambda", while in non-lambda procs "return means exit from embracing method".

    Four ways to make one

    Spelling Result
    Proc.new { 5 } proc
    proc { 5 } proc
    lambda { 5 } lambda
    -> { 5 } lambda, written with the "stabby" arrow

    The arrow form is the most common for lambdas today, and it takes its parameters in parentheses, as in ->(x) { x * 2 }. All of these can be called with .call(4), .(4) or [4]:

    double = ->(n) { n * 2 }
    p double.call(4), double.(4), double[4]
    # => 8
    # => 8
    # => 8
    

    Passing them where a block goes

    Put & in front of a proc or lambda to hand it to a method as its block. The documentation on calling methods calls this the block conversion operator:

    double = ->(n) { n * 2 }
    p [1, 2, 3].map(&double)
    # => [2, 4, 6]
    

    A lambda keeps its strictness when you do this. [[1, 2]].map(&lm) with the two parameter lambda above raises an ArgumentError, because map hands over one array, whereas [[1, 2]].map(&sq) quietly works. That is the same rule as before in a new place. If you have not met blocks yet, start with what a block is and what yield does, then come back. The earlier thread on map, each and select shows these methods taking blocks.

    Mistakes beginners make

    Calling a lambda with the wrong number of arguments. You get an ArgumentError with given and expected numbers. Count the parameters.

    Calling a proc and getting nil instead of an error. With sq.call(1) above, y was nil, and the code kept going. If a result looks oddly blank, check whether a proc is swallowing a missing argument.

    Using return in a proc outside any method. A proc that returns after its method is finished has nowhere to go. Ruby raises LocalJumpError (the docs call it an orphaned proc):

    def make_orphan
      proc { return 1 }
    end
    
    make_orphan.call
    # LocalJumpError: unexpected return
    

    A lambda cannot be orphaned, because its return only leaves itself.

    Thinking &:upcase is a different thing. The &:name shorthand turns a symbol into a proc via to_proc, so ["a"].map(&:upcase) works. It is the same & conversion as above. Symbols are covered earlier.

    Check this in order

    1. Run thing.lambda? to find out which kind you hold.
    2. Read the arity with thing.arity: lm.arity is 2 for a two parameter lambda.
    3. If the error is wrong number of arguments, it is almost certainly a lambda. Pass the right count, or switch to a proc if looseness is what you want.
    4. If a method stops early and you cannot see why, search it for return inside a proc or block.
    5. Unsure which to use? Start with a lambda, and switch only if you need the block-like behavior.

    The tour of blocks in Why's (Poignant) Guide to Ruby, chapter 4 on this site uses blocks with map and select, and everything here builds on the same idea. Checked on 12 November 2026.

    0
    ReplyLink

Plain text or simple Markdown. Links are nofollow. Your email is never shown.

Poignant Guide Ruby Q&A. Questions and answers on learning Ruby and programming: syntax, blocks, objects, gems, Rails, debugging and reading the Poignant Guide.