O'Reilly logo

Ruby by Example by Kevin C. Baird

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

#24 Nesting lambdas

Let’s review Procs for a bit. Procs are just functions that can be treated as data, what functional programming languages call first-class functions. Functions can create Procs; we saw that both make_incrementer and return_proc return Procs of different sorts. Given all that, what prevents us from making a Proc that returns another Proc when called? Nothing at all.

In the make_exp example below, we create specific versions of Procs that raise an argument to some specified power. That power is the exp argument taken by the outer lambda, which is described as a free variable because it is not an explicit argument to the inner lambda.

The inner lambda, which is returned, has a bound variable called x. It is bound because it is an ...

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