[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Different semantics of Proc and method call -- bug or feature?

Tammo Freese

8/2/2006 12:35:00 PM

Hi all,


I stumbled over the following difference between method calls and
Proc calls:

require 'pp';

def a_method(arg, *args)
arg
end

a_proc = Proc.new do | arg, *args |
arg
end

pp a_method(1) # => expected 1, got 1
pp a_proc.call(1) # => expected 1, got 1

pp a_method([1]) # => expected [1], got [1]
pp a_proc.call([1]) # => expected [1], got 1 <== ?!


I would expect that method calls and proc calls do not differ in such
a way.
Is this a bug or a feature?


Thanks for your help,

Tammo

16 Answers

Robert Klemme

8/2/2006 1:06:00 PM

0

Tammo Freese wrote:
> Hi all,
>
>
> I stumbled over the following difference between method calls and Proc
> calls:
>
> require 'pp';
>
> def a_method(arg, *args)
> arg
> end
>
> a_proc = Proc.new do | arg, *args |
> arg
> end
>
> pp a_method(1) # => expected 1, got 1
> pp a_proc.call(1) # => expected 1, got 1
>
> pp a_method([1]) # => expected [1], got [1]
> pp a_proc.call([1]) # => expected [1], got 1 <== ?!
>
>
> I would expect that method calls and proc calls do not differ in such a
> way.
> Is this a bug or a feature?

I think it's rather a feature although I can understand your surprise.
IIRC block (and thus proc) parameters behave more similar to assignments:

>> def t
>> yield 1
>> yield [2]
>> yield [3,4]
>> end
=> nil
>> t {|a| p a}
1
[2]
[3, 4]
=> nil
>> t {|a,*b| p a}
1
2
3
=> nil
>> t {|a,| p a}
1
2
3
=> nil

15:04:11 [~]: ruby -e 'a=1; p a; a=[2,3]; p a'
1
[2, 3]
15:04:45 [~]: ruby -e 'a=1; p a; a=[2]; p a; a=[3,4]; p a'
1
[2]
[3, 4]
15:05:08 [~]: ruby -e 'a,*b=1; p a; a,*b=[2]; p a; a,*b=[3,4]; p a'
1
2
3
15:05:25 [~]: ruby -e 'a,=1; p a; a,=[2]; p a; a,=[3,4]; p a'
1
2
3
15:05:34 [~]:

HTH

Kind regards

robert


Morton Goldberg

8/2/2006 2:03:00 PM

0

This _is_ strange. R. Klemme's explanation is enlightening, but now I
wonder why qq in the following works like a method definition rather
than like pp?

#! /usr/bin/ruby -w

pp = Proc.new do |arg, *args|
p [arg, args]
end

qq = lambda do |arg, *args|
p [arg, args]
end

s = 1
v = [2]

puts VERSION => 1.8.2

pp.call s => [1, []]
pp.call v => [2, []]
pp.call v, v => [[2], [[2]]]

qq.call s => [1, []]
qq.call v => [[2], []]
qq.call v, v => [[2], [[2]]]

Regards, Morton

On Aug 2, 2006, at 8:35 AM, Tammo Freese wrote:

> Hi all,
>
>
> I stumbled over the following difference between method calls and
> Proc calls:
>
> require 'pp';
>
> def a_method(arg, *args)
> arg
> end
>
> a_proc = Proc.new do | arg, *args |
> arg
> end
>
> pp a_method(1) # => expected 1, got 1
> pp a_proc.call(1) # => expected 1, got 1
>
> pp a_method([1]) # => expected [1], got [1]
> pp a_proc.call([1]) # => expected [1], got 1 <== ?!
>
>
> I would expect that method calls and proc calls do not differ in
> such a way.
> Is this a bug or a feature?
>
>
> Thanks for your help,
>
> Tammo
>


Yukihiro Matsumoto

8/2/2006 2:05:00 PM

0

Hi,

In message "Re: Different semantics of Proc and method call -- bug or feature?"
on Wed, 2 Aug 2006 21:35:05 +0900, Tammo Freese <freese@acm.org> writes:

|I would expect that method calls and proc calls do not differ in such
|a way.
|Is this a bug or a feature?

It's a feature, but we are trying to fix it in the next major release
(1.9 or 2.0).

matz.

James Gray

8/2/2006 2:15:00 PM

0

On Aug 2, 2006, at 9:05 AM, Yukihiro Matsumoto wrote:

> It's a feature, but we are trying to fix it in the next major release
> (1.9 or 2.0).

Matz, could you tell us why you are considering releasing the next
major version as 1.9 instead of 2.0?

James Edward Gray II

Yukihiro Matsumoto

8/2/2006 2:23:00 PM

0

Hi,

In message "Re: Next Major Version (was Re: Different semantics of Proc and method call -- bug or feature?)"
on Wed, 2 Aug 2006 23:14:32 +0900, James Edward Gray II <james@grayproductions.net> writes:

|On Aug 2, 2006, at 9:05 AM, Yukihiro Matsumoto wrote:
|
|> It's a feature, but we are trying to fix it in the next major release
|> (1.9 or 2.0).
|
|Matz, could you tell us why you are considering releasing the next
|major version as 1.9 instead of 2.0?

In the current pace, it would take years to release 2.0, but some
requested features we already made in 1.9 branch. We felt the request
is reasonable.

matz.

S Wayne

8/2/2006 3:39:00 PM

0

In keeping with your current numbering scheme, does this mean there
will be a ruby 1.10 release?

On 8/2/06, Yukihiro Matsumoto <matz@ruby-lang.org> wrote:
> Hi,
>
> In message "Re: Next Major Version (was Re: Different semantics of Proc and method call -- bug or feature?)"
> on Wed, 2 Aug 2006 23:14:32 +0900, James Edward Gray II <james@grayproductions.net> writes:
>
> |On Aug 2, 2006, at 9:05 AM, Yukihiro Matsumoto wrote:
> |
> |> It's a feature, but we are trying to fix it in the next major release
> |> (1.9 or 2.0).
> |
> |Matz, could you tell us why you are considering releasing the next
> |major version as 1.9 instead of 2.0?
>
> In the current pace, it would take years to release 2.0, but some
> requested features we already made in 1.9 branch. We felt the request
> is reasonable.
>
> matz.
>
>

Matt Todd

8/2/2006 4:10:00 PM

0

What was it Mauricio had announced... Version 1.z or was it 1.Gamma
sometime in 2017?

;)

M.T.

Yukihiro Matsumoto

8/3/2006 2:29:00 AM

0

Hi,

In message "Re: Next Major Version (was Re: Different semantics of Proc and method call -- bug or feature?)"
on Thu, 3 Aug 2006 00:38:41 +0900, "N Okia" <wrecklass1@gmail.com> writes:

|In keeping with your current numbering scheme, does this mean there
|will be a ruby 1.10 release?

The version letters will be stay in one letter each, so that 1.a will
be more likely than 1.10.

matz.

Rick DeNatale

8/3/2006 3:27:00 PM

0

On 8/2/06, Yukihiro Matsumoto <matz@ruby-lang.org> wrote:
> Hi,
>
> In message "Re: Next Major Version (was Re: Different semantics of Proc and method call -- bug or feature?)"
> on Thu, 3 Aug 2006 00:38:41 +0900, "N Okia" <wrecklass1@gmail.com> writes:
>
> |In keeping with your current numbering scheme, does this mean there
> |will be a ruby 1.10 release?
>
> The version letters will be stay in one letter each, so that 1.a will
> be more likely than 1.10.
>
> matz.

Matz,

I hesitate to ask YOU this, but are you really sure you want to go that way.

Almost every software package I've run across keeps each level of the
version "number" as a number, so 1.10 would be much more in line with
standard practice than 1.a

--
Rick DeNatale

James Gray

8/3/2006 3:31:00 PM

0

On Aug 3, 2006, at 10:26 AM, Rick DeNatale wrote:

> On 8/2/06, Yukihiro Matsumoto <matz@ruby-lang.org> wrote:
>> Hi,
>>
>> In message "Re: Next Major Version (was Re: Different semantics of
>> Proc and method call -- bug or feature?)"
>> on Thu, 3 Aug 2006 00:38:41 +0900, "N Okia"
>> <wrecklass1@gmail.com> writes:
>>
>> |In keeping with your current numbering scheme, does this mean there
>> |will be a ruby 1.10 release?
>>
>> The version letters will be stay in one letter each, so that 1.a will
>> be more likely than 1.10.
>>
>> matz.
>
> Matz,
>
> I hesitate to ask YOU this, but are you really sure you want to go
> that way.
>
> Almost every software package I've run across keeps each level of the
> version "number" as a number, so 1.10 would be much more in line with
> standard practice than 1.a

But it can't be compared properly using Ruby operators:

>> "1.8.5" < "1.10.0"
=> false

James Edward Gray II