Aren't closures just grand? Check out this blog from Martin Fowler on
closures in Ruby:
http://martinfowler.com/bliki/CollectionClosureMethod.html
FWIW, I believe that Martin uses the term "closure" incorrectly on
that entry, as well as on:
http://martinfowler.com/bliki/Closure.html
Ruby's blocks have three great things going for them:
1) They have a very, very simple syntax (and are anonymous)
2) They are first-class functions (can be treated and passed as
variables)
3) Ruby's syntax allows blocks to be passed to a method very easily
4) They are closures
A function that is a closure has access to the lexical scoping in
which it was defined (the local variables). JavaScript's and Lua's
functions are #2 and #4 (first-class closures), but they are
certainly not easy to create nor (as easy) to pass. [1]
The article you cite rhapsodizes about features 1-3 of Ruby's blocks,
but never uses the fact that they are closures to do its magic.
Although I love and grok closures, even Matz has stated that they're
not vital to Ruby:
"Yes, and that sharing [between closures and their scope] allows
you to do some interesting code demos, but I think it's not that
useful in the daily lives of programmers. It doesn't matter that
much. The plain copy, like it's done in Java's inner classes for
example, works in most cases. But in Ruby closures, I wanted to
respect the Lisp culture." [2]
Mostly I'm just being pedantic about the usage of the term "closure".
What Martin is describing in his article are called "blocks" in Ruby.
I suggest only using the term "closure" when you are specifically
describing the features of a lexical closure. For more information,
see the Wikipedia entry on closures [3] and this in-depth article on
closures in JS [4].
[1] Compare Ruby's: my_array.each{ |el| puts el }
with JavaScript's: my_array.each( function( el ){ alert( el ) } )
or Lua's: each( my_table, function( el ) print( el ) end )
[2]
http://www.artima.com/intv/closures2.html
[3]
http://en.wikipedia.org/wiki/Closure_(computer_science)
[4]
http://jibbering.com/faq/faq_notes/closures.html