Lambdas—Same, But Different

Procs have a lot of similarities with lambdas. In fact, you might be surprised to learn that a lambda is a Proc:

 >>​ lambda {}.class
 =>​ Proc

A Proc, however, is not a lambda:

 >>​ proc {}.class
 =>​ Proc

Fortunately, Ruby has a helpful predicate method that lets you disambiguate procs and lambdas via the Proc#lambda? method:

 >>​ lambda {}.lambda?
 =>​ ​true
 
 >>​ proc {}.lambda?
 =>​ ​false

You might be wondering why there isn’t a corresponding proc? method. Some thinking leads you to realize that this method would be pretty useless. Let’s assume the method existed:

 # NOTE: This method doesn't exist!
 >>​ lambda {}.proc?
 =>​ ​true
 
 # NOTE: This method doesn't exist!
 >>​ proc {}.proc? ...

Get Mastering Ruby Closures now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.