Explain Proc.new vs. lambda in Ruby like I'm five

Did you find this post useful? Show some love!

A lambda is a kind of proc.

proc.class # returns 'Proc'
lam.class  # returns 'Proc'

That doesn't really matter, but it's worth noting.

Both are one-off bits of code you can pass around a little bit more easily than, say, a method or block.

And between the two, a Proc acts more like a "block" (e.g. .each do) and lambda acts more like a "method" (e.g. def new_method).

And those differences are:

Lambdas and methods check the number of arguments (meaning you could get the ArgumentError: wrong number of arguments (3 for 1) error and they also return from themselves, and not the enclosing method. Blocks and procs return from the greater scoped method.

By returning from itself vs


def some_method
  Proc.new { |x| return "hey hey" } # This would just return from the whole enclosing method
  lambda { |x| return "hey hey" } # This would just return from itself and not end the whole method

I think that's basically accurate. The good news is you can definitely get far without having this stuff down for sure.

If I got this wrong I'd love some additional feedback.

Classic DEV Post from Apr 30

What is your favourite git merge tool/strategy?

VSCode is my favourite editor. But while rebasing and resolving merge conflicts...

Follow @flexdinesh to see more of their posts in your feed.
Tiffany Wismer
I was an Admin Assistant and a Barista and now I'm learning code because I'm tired of talking to people. Just kidding. But also not kidding.
More from @tiffanywismer
Explain React.js Like I'm Five
#explainlikeimfive #react #javascript
Trending on dev.to
Explain RxJS Like I'm Five
#explainlikeimfive #javascript
How much should you refactor names when domain terminology changes?
#discuss #refactoring
Devs using font ligatures, what's the selling point?
What's your worst estimate story?
#discuss #story #estimate #carreer
The Shell Introduction I Wish I Had
#shell #productivity #beginners #bash
Why I love hiring Junior engineers
#engineers #culture #career
How do you keep track of what you're reading?
Learn New Technology as a Way of Stepping Up Your Understanding of Your Current One