Syed Humza Shah


Engineering leader. Likes coffee. Loves to travel. Runs on a combination of optimism and pragmatism.

Read more about him here.

Memorize How Ruby's Lambdas and Procs treat 'return'

While the number-of-arguments rule is easy, I’ve been unable to memorize which is which when it comes to the treatment of ‘return’. This is primarily because I rarely use ‘return’ within procs and lambdas. But I’ve figured out a neat memory-association technique to help me remember the rule:

# ruby code

puts -> { return 1 }.call

That works because lambda ‘returns’ on the block level. Replace that with a proc and watch it fail because proc ‘returns’ on a method level - doing that in the arguments would cause a LocalJumpError.

From now on, all I have to do is recall the above example and easily infer the ‘return’ difference between lambdas and procs!