Joel VanderWerf
4/17/2008 9:59:00 PM
Avdi Grimm wrote:
> On Thu, Apr 17, 2008 at 5:35 PM, Stedwick <philip.brocoum@gmail.com> wrote:
>> Can somebody give me an extremely useful, NOT complicated, example of
>> when lambdas are the absolute perfect solution to a problem?
>
> Every time you use a block in ruby you're using a lambda implicitly.
Implicitly, but not really. I wonder, was the original question more
like "what are blocks for?" or "yes, I know what blocks are for, but why
are there also these Proc objects, which are instantiated with the
lambda construct?"
If the latter, then one answer is: when you need the program state that
is encapsulated in a block to persist outside of the method in which it
is used. For example, this happens in event-based frameworks: a setup
method in a client class registers lambdas as event handlers; the
framework doesn't need to know the classes or methods of the client
class, hence loose coupling. As a bonus, these handlers can refer to
shared program state (local variables) in the setup method.
--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407