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