Christopher Dicely wrote:
When you yield to a block attached to a function -- which, you must
keep in mind, isn't a proc or a lambda or even a normal Ruby object at
all, its a different beast entirely though you can call a method with
the method(&proc) notation to send a Proc instance (proc or lambda) to
be wrapped in a block* -- it behaves like a proc (that is an object
created with Proc.new {...} in any version of ruby, and also proc
{...} in Ruby 1.9.x.) Both "lambda_and_yield" and "proc_and_yield",
therefore, aren't yielding to lambdas or procs, the methods yield to a
block in both cases
But the object_id is the same, so it's not wrapped in a "block" object.
And neither, I think, is it mutated. There is "is_lambda?" in 1.9, but
no "is_block?"
which
suggests to me that a call to the Proc instance gets wrapped in a
block if the method doesn't use the &block argument pattern to get it
as a Proc instance.
But the only way to get hold of the block argument without &block is to
use yield. So it is equivalent to say that yield has special semantics.
The Pickaxe (2nd Ed.) does discuss the behavior of
blocks, procs (which it calls "raw procs"), and lambdas in quite some
detail, on pp. 356-360.
It's somewhat incomplete. p357-p358
"There are three ways of converting a block into a Proc object:
1. By passing a block to a method whose last parameter is prefixed with
an ampersand. That parameter will receive the block as a Proc object"
But it doesn't say what happens if you pass a lambda, using the &
notation at the caller.
It says that a block is not an object at all, and it goes on to make a
distinction between two types of real objects: a "raw proc" and a
"lambda", which are the two flavours of Proc object.
I'm not sure whether it makes much sense to consider a block as a third
class of entity, which is not an object at all. If I do
def receiver(&blk)
STDERR.puts blk.object_id
end
foo = lambda { "Whoa" }
STDERR.puts foo.object_id
receiver(&foo)
then blk in receiver is exactly the same object as foo at the caller.
···
--
Posted via http://www.ruby-forum.com/\.