Hi --

On Mon, 6 Jul 2009, Joel VanderWerf wrote:

> Russ McBride wrote:
>> Thanks.  Interesting discussion.
>> 
>>> given &expr, if expr is a Proc object, use it as
>>> the block; if not, call to_proc on it.
>> ... and then use the resulting proc as a block.
>> 
>> Maybe the best way to think of & is a a proc/block converter, which,
>> if called on something that is neither looks first for a to_proc method 
>> before
>> performing its conversion.
>
> That still makes & sound like an operator method that can be called on an 
> object to produce a different object, though. Blocks are syntactic entities, 
> Procs are semantic entities (a.k.a. values, objects), and & is a way of 
> relating them. The conversion from one class to another is kind of 
> distracting here, because it really only needs to happen in the special cases 
> like &:sym .

It's not exactly a conversion, though, since as you point out, blocks
aren't objects. In the &expr scenario (I almost typed &expr; as I've
been typing XML all evening :-) there has to be an actual Proc object
involved. So it's a kind of normalization, rather than conversion, if
that makes sense.

> The & is really more like variable assignment ( = ) in ruby rather than an 
> operator, in that '=' affects bindings (another syntax-semantics relation) 
> but does not operate on objects. This is something that often trips up people 
> coming to ruby from C et al.

I think one issue is that there's no good term for the Proc that gets
pressed into service as a block. It's not exactly a block, but just
calling it a Proc doesn't serve to differentiate it from a Proc passed
as a method argument.

The common practice of using &block as the variable that "catches" the
code block is kind of imprecise for the same reason: it's not a block
that's bound to that variable, but a Proc. The term "block" tends to
get spread out to mean more things than the block itself (the
syntactic construct), which I think is kind of too bad but I'm not
sure what the alternative is.


David

-- 
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Now available: The Well-Grounded Rubyist (http://manning.com/black2)
Training! Intro to Ruby, with Black & Kastner, September 14-17
(More info: http://rubyurl.com/vmzN)