Two arguments for accepting Proc#yield.
A. Arguing against David’s logic (above)
···
On Saturday, April 26, 2003, 10:03:28 PM, dblack wrote:
In a ‘yield’, the block is being yielded to – it is not, itself,
yielding control. Therefore, what you’d really need, to describe a
yield in terms of the block as a receiver, would be something like:
blk.be_yielded_to
which I’m not advocating, of course; it’s just that
blk.yield
suggests that the block is performing an action described as
'yield’ing, which I don’t think it is.
The same argument applies to Proc#call.
p = proc { |x| x + 3 }
p.call(2) # → 5
Really, a more accurate name would be
p.be_called(2)
In David’s language:
“blk.call” suggests that the block is performing an action described
as 'call’ing, which I don’t think it is.
B. Arguing positively from first principles
The two methods following are functionally equivalent.
def foo1(n, &block)
block.call(n+1)
end
def foo2(n)
yield n+1
end
They are effectively the same, and they both make sense. But what is
that “yield” really? It’s like a method call with an implicit
receiver. The receiver is the block passed to the method call.
If we are happy with “yield” (the keyword) being an implicit method
call, why can’t we allow it to be an explicit method call, in which it
does the same thing?
Concluding notes
The keyword “yield” was probably borrowed from another language, but
it helps to consider what it really means, which is “produce”. This
makes sense as a keyword because it is producing a value, or list of
values, to be consumed by an anonymous block.
Therefore, “yield” is a special case issue of “giving way” (another
meaning of yield) to the parent method, temporarily. It really is the
perfect name for what it does. But it’s not the perfect name to
describe mvalues, avalues and parameters. That’s where David et al
have a good point.
But it’s a different point from what is discussed (twice) above.
Cheers,
Gavin