[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: oddity with block argument passing

Carlos

12/23/2005 2:31:00 AM

[Gustav Munkby <grddev@gmx.net>, 2005-12-20 21.40 CET]
[...]
> Constructs = [
> lambda {|x, *y| [x, y]},
> proc {|x, *y| [x, y]},
> Proc.new {|x, *y| [x, y]}
> ]
>
> puts "ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}]"
> Constructs.each{ |x| puts x[[]].inspect }
>
> Below is the output of this script from the two versions of ruby where
> I've tried it.
>
> $ ruby proc_test.rb
> ruby 1.8.3 (2005-09-21) [i686-linux]
> [[], []]
> [[], []]
> [nil, []]
>
> $ ruby-cvs proc_test.rb
> ruby 1.9.0 (2005-12-20) [i686-linux]
> [[], []]
> [nil, []]
> [nil, []]

Compare:

def a x, *y
[x, y]
end

a [] # => [[], []]

x, *y = []

[x, y] # => [nil, []]

In one case, values are assigned as in normal assignment; in others, as in
method parameter assignment. The "normal variable assignment" is to be
expected in normal blocks; the ones created with lambda use parameter-like
assignment.

> Is this to be expected?

Many people expect different things here, and you can find a lot of
discussions about this topic in the list archives.

HTH.


2 Answers

gustav munkby

12/26/2005 11:41:00 AM

0

angus wrote:
> [Gustav Munkby <grddev@gmx.net>, 2005-12-20 21.40 CET]
> [...]
>
>> Constructs = [
>> lambda {|x, *y| [x, y]},
>> proc {|x, *y| [x, y]},
>> Proc.new {|x, *y| [x, y]}
>> ]
>>
>> puts "ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}]"
>> Constructs.each{ |x| puts x[[]].inspect }
>>
>>Below is the output of this script from the two versions of ruby where
>>I've tried it.
>>
>> $ ruby proc_test.rb
>> ruby 1.8.3 (2005-09-21) [i686-linux]
>> [[], []]
>> [[], []]
>> [nil, []]
>>
>> $ ruby-cvs proc_test.rb
>> ruby 1.9.0 (2005-12-20) [i686-linux]
>> [[], []]
>> [nil, []]
>> [nil, []]
>
>
> Compare:
>
> def a x, *y
> [x, y]
> end
>
> a [] # => [[], []]
>
> x, *y = []
>
> [x, y] # => [nil, []]
>
> In one case, values are assigned as in normal assignment; in others, as in
> method parameter assignment. The "normal variable assignment" is to be
> expected in normal blocks; the ones created with lambda use parameter-like
> assignment.
>

thanks for this explanation, now i really understand why there's a
difference between the parameter-like assignment and normal variable
assignment.

they way you say it, it sounds as if the difference should be between
lambda and proc, but in 1.8.3 the difference is between Proc.new and
proc, does this mean that there is an issue with Kernel.proc in 1.8.3?

and i would also suggest changing the api-documation, allthough i don't
know exactly i shall proceed to propose such a change. for Kernel.lambda
it currently states:

Equivalent to Proc.new, except the resulting Proc objects check the
number of parameters passed when called.

a better phrasing i think would be:

Equivalent to Proc.new, except the resulting Proc objects have
method-like-parameter-passing instead of the normal
expression-like-parameter-passing.

or something along those lines. personally i would think it'd be nice
with something more explaining the differences of the two, but the
api-documentation might not be the proper place for making this clear.

!g


dblack

12/26/2005 12:57:00 PM

0