Hi --
Bala Paranj wrote:
So this block thingy is kinda like anonymous classes?
In principle they are anonymous *methods* rather than *classes*. If you
think of them like that it should be easier to understand what's going
on when you pass arguments into them and these arguments initialize the
block variables between the upright bars (like |cel| in the example
above).
Thinking of them as methods has some pitfalls, though, since methods
always live in modules or classes and blocks don't.
Blocks are kind of oddities (in my view!) in Ruby, however, as they
aren't objects. True, they can be wrapped up inside objects (Procs) but
that doesn't alter the fact that a block itself has no independent
existence. This contrasts with some other object oriented languages -
notably Smalltalk - in which a block is an object with its own methods.
Ask a standalone Smalltalk block to tell you its class and it will reply
that it is an instance of Block (or BlockClosure). Ask a standalone Ruby
block to tell you its class - e.g.
puts( {|cel| cel * 9 / 5 + 32}.class )
...and Ruby replies:
parse error, unexpected '|', expecting '}'
puts( {|cel| cel * 9 / 5 + 32}.class )
However, I may be straying into arcane areas here 
I think the best way to look at blocks is as a part of the syntax of
the Ruby method call:
[receiver dot] method_name [arg_list] [block]
Informally one does speak of blocks as anonymous functions, rather
than saying "a syntactic construct that gets wrapped in a Proc" (or
whatever). But it's useful to keep the distinction in view that they
start life as syntax.
On that construction, a block is a kind of sibling to the argument
list -- and, in both cases, they're pure syntax: there's no Block
class and no ArgumentList class. Also in both cases, the method can
grab what's there and stash it in a variable:
m(a,b,c) { puts "hi" }
> > > >
def m(x,y,z,&block)
In a sense, the &block thing is really a completely separate
transaction from the x,y,z part; it might almost be represented as:
def m(x,y,z) {&block}
or something. (I'm not advocating that, just making the point that
the block and &block are going on in parallel to, rather than as part
of, the passing of arguments.)
David