Getting down with Closures, Blocks, and Procs
Paul Cantrell has created a cool guide, with demonstrations, of the powers, quirks, and surprises of closures, blocks, and procs in Ruby. He writes:
A closure is a block of code which meets three criteria:
* It can be passed around as a value and
* executed on demand by anyone who has that value, at which time
* it can refer to variables from the context in which it was created (i.e. it is closed with respect to variable access).
Closures are a mainstay of functional languages, but are present in many other languages as well (e.g. Java's anonymous inner classes). You can do cool stuff with them: they allow deferred execution, and some elegant tricks of style.
Ruby is based on the "principle of least surprise," but I had a really nasty surprise in my learning process. When I understood what methods like "each" were doing, I thought, "Aha! Ruby has closures!" But then I found out that a function can't accept multiple blocks -- violating the principle that closures can be passed around freely as values. This document details what I learned in my quest to figure out what the deal is.
All but the highest Ruby masters are going to find at least one thing in here that makes them scratch their head for a few seconds and go "Ah, yes!".
He walks all the way through closure and block land through to generating a Lisp style enumerable class with lazy evaluation. Language geeks, enjoy!