O'Reilly logo

Mastering Ruby Closures by Benjamin Tan Wei Hao

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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? ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required