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

Get Ruby by Example 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.