4 ms·
IME, there are relatively few good uses of lambdas in Ruby: blocks can do almost everything that lambdas can, but with better syntax support and closer to the "
by woodruffw 5y ago
IME, there are relatively few good uses of lambdas in Ruby: blocks can do almost everything that lambdas can, but with better syntax support and closer to the "idiomatic" patterns that most real Ruby uses. For the things that blocks can't do, there are procs (which the article correctly observes are very similar to lambdas).
A good example from the article:
> If you want to map over a collection, but the map function changes based on the input, you can use a lambda to encapsulate the changing logic.
You can do this with `Hash.new { |x| ... }`! The block version is more readable, more idiomatic, and it gives you free memoization.
- vidarh 5y agoI wish people would stop seeing blocks as different. Lambda and proc in Ruby both produce instances of Proc. That blocks are not Proc to instances in MRI until you name them is an optimization only, and nothing stops an implementation from making blocks Proc instances (indeed my woefully incomplete Ruby compiler does that). I agree with you that block syntax means you're less likely to need to explicitly pass lambda/procs around, but part of the reason people get confused about this is seeing blocks as something more different than they are.