What is the difference between a proc and a lambda in Ruby?
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.
Cite
Poignant 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.- Odile MarchettiStaffStaff: 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 # => trueThose 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, withArgumentErrorfor 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 # => 20In the proc version,
returnexits the wholerun_procmethod, so line20never runs. In the lambda version,returnonly exits the lambda, and the method carries on to20. The documentation puts it this way: in lambdas "returnandbreakmeans exit from this lambda", while in non-lambda procs "returnmeans 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 # => 8Passing 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, becausemaphands 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
ArgumentErrorwithgivenandexpectednumbers. Count the parameters.Calling a proc and getting
nilinstead of an error. Withsq.call(1)above,ywasnil, 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 returnA lambda cannot be orphaned, because its return only leaves itself.
Thinking
&:upcaseis a different thing. The&:nameshorthand turns a symbol into a proc viato_proc, so["a"].map(&:upcase)works. It is the same&conversion as above. Symbols are covered earlier.Check this in order
- Run
thing.lambda?to find out which kind you hold. - Read the arity with
thing.arity:lm.arityis2for a two parameter lambda. - 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. - If a method stops early and you cannot see why, search it for
returninside a proc or block. - 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.
- Run
Write something first.
Give people something to work with: at least 30 words on what happened and what you tried.
That is too long. Keep it under 6,000 characters.
Write the question as the title, 15 to 140 characters, no links.
Pick a category.
Add a name (2 to 40 characters, no links).
That email address does not look right.
That was quick. Read the thread, then try again.
The form expired. Reload the page and post again.
Something went wrong with the form. Reload and try again.
Please complete the check and post again.
Limit reached for now. Try again later.
This thread is closed to new replies.
This thread no longer accepts replies.
Something went wrong with the form. Reload and try again.
Post a reply or question first (name and email), then this browser can vote, edit and accept answers.
You cannot vote on your own post.
Only the author (within 30 days) or the forum team can do that.
That email already has an account. Use your sign-in link instead.
New accounts are paused for the moment. Try again later.
That was already posted.