Assigning a block to a variable in Ruby

On Dec 15, 2005, at 5:41 PM, [email protected] wrote:

Except… they’re not quite identical, and actually as of RubyConf
2003 (I think) Matz agreed that ‘proc’ would be deprecated in favor of
‘lambda’, because having something called proc and something called
Proc.new that weren’t the same was confusing.

This is an area that has been a bit fuzzy for me.

Is it fair to say that the only difference between
Proc.new {#some code}
and
lambda {#some code}

is the behavior when ‘return’ is explicitly called in the block?
Are there any other differences?

Am I correct in saying that when a formal argument list explicitly
captures a block:
def foo(&block);end
that Ruby converts the actual block to a Proc instance via
Proc.new as opposed to Kernel#lambda?

And finally, is there a way to create a lambda-like object via
a class method of Proc or is that behavior only available via
Kernel#lambda? I seems unusual that Kernel#lambda returns an
instance of Proc but that there is no way to get Proc itself
to generate that type of an object.

Mark J. Reed [email protected] wrote:

method into a closure value, but that’s not how they live, and methods
do not occupy variable namespace. So defining a method ‘foo’ does not
have any affect on the variable
‘foo’, or vice versa:

Actually, what I meant was that ruby does not store the source code of
the method anywhere, so it cannot be introspected.

martin

C Erler wrote:

On 15/12/05, [email protected] [email protected] wrote:

In fact, perusing the documentation, I can’t see how I would have
figured out the alternative mechanism using proc in lowercase - where
is this documented?

It’s a Kernel (which has a bunch of generally useful methods, like
puts) method. Strangely, the core API doesn’t seem to have Kernel,

This is a known issue with 1.8.3. The docs for Kernel are missing…

-Justin

Technically, Ruby is really an ‘incremental compiler’. That there is no
final code generation step actually doesn’t change this.

Normally we make the following distinctions

An INTERPRETER reads source code and processes each symbol in the code
each time it encounters the symbol. This is very inefficient, of
course. Thus a loop with an inner expression will require that the
inner expression be parsed and transformed into an evaluation stack
each time it is encountered. Early versions of BASIC were pure
interpreters, and performance was consequently far removed from native
code.

An INCREMENTAL COMPILER (most scripting languages fall into this
category) transforms source into an internal structure with a symbol
table and expression stacks etc as it reads the source. Subsequently
this structure is modified, but not discarded, if the source is changed
or additional source code is fed into the system. (The first popular
example of an incremental compiler was Microsoft QuickBasic, as far as
I can recall. At the time, this was a miracle to behold). The internal
representation can then be ‘executed’, usually by what is in effect a
virtual machine within the system.

A TRUE COMPILER normally operates as a batch process. All the tasks
that an incremental compiler performs are done once on the whole source
tree. Subsequently, the code generator emits code either for the native
hardware architecture or for a virtual machine. Source code changes
require that the entire batch process be repeated.

There are also ‘just in time’ compilers for virtual machines which can,
at runtime, evaluate ‘hotspots’ in the code which would benefit from
further compilation - more technically, translation - into native
machine code.

It is because modern scripting languages are incremental compilers and
not merely interpreters that their performance is so good. If they were
pure interpreters they would be hundreds of times slower.

Possibly Wikipedia could benefit from some clarification in its
explanation.

On Dec 16, 2005, at 8:17 AM, [email protected] wrote:

So Proc has no knowledge of that
specialization. I agree that it should be of a different class. I’ve
been seeing people (including myself) having a hard time keeping all
of this straight for years. Not that the users have to be coddled :slight_smile:
but I think there’s evidence that it is indeed confusing.

Thanks for the clarifications Dave. I wonder if some sort of
unification
of all these rules/behaviors is possible. I’m not sure exactly what I
mean by that, maybe it is just some better documentation, maybe it is
something else…

There’s actually no Kernel#lambda (lambda is a keyword, not a method),

I assume this is so the parser can make special arrangements while
constructing the AST rather then waiting until the code is actually
executed. I think matz has mentioned changing eval from a method to a
keyword for this reason, no?

Then… possibly you should add such clarifications if yours are in
fact clear?

I can’t speak to your accuracy, I’m just saying… it’s a wiki… ahem

Hi –

On Fri, 16 Dec 2005, [email protected] wrote:

On Dec 15, 2005, at 5:41 PM, [email protected] wrote:

Except… they’re not quite identical, and actually as of RubyConf
2003 (I think) Matz agreed that ‘proc’ would be deprecated in favor of
‘lambda’, because having something called proc and something called
Proc.new that weren’t the same was confusing.

This is an area that has been a bit fuzzy for me.

And for many of us :slight_smile:

Is it fair to say that the only difference between
Proc.new {#some code}
and
lambda {#some code}

is the behavior when ‘return’ is explicitly called in the block?
Are there any other differences?

I think the matter of arity strictness is still different. Frankly I
could never remember the details, and I know that I am by no means
alone in this. Basically, lambdas will give you an error if you send
them the wrong number of arguments. Unless they take only one
argument, in which case you’ll get a warning. If the lambda takes no
arguments, then it doesn’t care, unless the no-arguments is specified
with || (instead of just nothing), in which case you have to send
exactly zero arguments.

Procs are different. They give you the warning sometimes, but I don’t
think they ever raise an exception because of wrong number of
arguments. (I really wish that warning would disappear.)

And so on. I’m not making fun of it, though it may sound that way. I
find it genuinely confusing, and it’s a bit notorious for this. If
anyone wishes to come along and claim that it’s simple and easy to
remember, please provide instructions for remembering it :slight_smile:

Am I correct in saying that when a formal argument list explicitly
captures a block:
def foo(&block);end
that Ruby converts the actual block to a Proc instance via
Proc.new as opposed to Kernel#lambda?

There’s actually no Kernel#lambda (lambda is a keyword, not a method),
but yes, it becomes a Proc. Although… if you do this:

meth &lambda { … }

it remains a lambda in the method (or so it appears from what irb is
telling me).

And finally, is there a way to create a lambda-like object via
a class method of Proc or is that behavior only available via
Kernel#lambda? I seems unusual that Kernel#lambda returns an
instance of Proc but that there is no way to get Proc itself
to generate that type of an object.

Well, lambda is a kind of specialization of Proc – almost in the
spirit of being a subclass. So Proc has no knowledge of that
specialization. I agree that it should be of a different class. I’ve
been seeing people (including myself) having a hard time keeping all
of this straight for years. Not that the users have to be coddled :slight_smile:
but I think there’s evidence that it is indeed confusing.

David


David A. Black
[email protected]

“Ruby for Rails: Ruby techniques for Rails developers”,
coming April 2006 (Ruby for Rails)

On Dec 16, 2005, at 12:22 PM, magnus wrote:

Then… possibly you should add such clarifications if yours are in
fact clear?

I can’t speak to your accuracy, I’m just saying… it’s a wiki…
ahem

I assume you mean Ruby garden (http://www.rubygarden.org/ruby)?
That would be a great place to add clarifications to all this stuff but
it isn’t the ‘official’ documentation so that doesn’t completely address
the issue.

My comfort level isn’t high enough yet to author something myself but
maybe soon…

On Sat, Dec 17, 2005 at 03:47:10AM +0900, [email protected] wrote:

That would be a great place to add clarifications to all this stuff but
it isn’t the ‘official’ documentation so that doesn’t completely address
the issue.

Actually, I think that was in reference to this:

“Possibly Wikipedia could benefit from some clarification in its
explanation.”


Chad P. [ CCD CopyWrite | http://ccd.apotheon.org ]

unix virus: If you’re using a unixlike OS, please forward
this to 20 others and erase your system partition.

[email protected] wrote:

interpreters, and performance was consequently far removed from native
virtual machine within the system.
machine code.

It is because modern scripting languages are incremental compilers and
not merely interpreters that their performance is so good. If they were
pure interpreters they would be hundreds of times slower.

Possibly Wikipedia could benefit from some clarification in its
explanation.

Don’t forget “dynamic translation,” a la Smalltalk. Code is compiled
for a virtual machine, but all VM instructions (not just hot spots) are
translated into native instructions the first time they are encountered.