Hello,
I always thought that the following 4 methods should be equivalent if
they
are called with one argument:
def t1(*a)
yield *a
end
def t2(*a, &blk)
blk.call(*a)
end
def t3(a)
yield a
end
def t4(a, &blk)
blk.call(a)
end
Well they don’t seem to be:
$ cat tst.rb
def t1(*a)
yield *a
end
def t2(*a, &blk)
blk.call(*a)
end
def t3(a)
yield a
end
def t4(a, &blk)
blk.call(a)
end
[:t1, :t2, :t3, :t4].each { |meth|
puts “================================”
p send(meth, [1, 2]) { |a,| a }
p send(meth, [1, 2]) { |a,b| [a, b] }
p send(meth, [1, 2]) { |a| a }
}
$ ruby -v
ruby 1.8.3 (2005-09-21) [i686-linux]
$ ruby tst.rb
[1, 2]
[[1, 2], nil]
[1, 2]
1
[1, 2]
[1, 2]
1
[1, 2]
[1, 2]
1
[1, 2]
[1, 2]
$ ./ruby-1.9 -v
ruby 1.9.0 (2005-12-28) [i686-linux]
$ ./ruby-1.9 tst.rb
[1, 2]
[[1, 2], nil]
[1, 2]
1
[1, 2]
[1, 2]
1
[1, 2]
[1, 2]
1
[1, 2]
[1, 2]
So there is a difference between using *a and a in the method parameter
list and a difference between using yield and block.call(). I would have
expected
[1, 2]
[[1, 2], nil]
[1, 2]
for all 4 cases. (I checked that send() is not the source of the
problem)
Is this intended this way? Should it be changed?
Interestingly yarv behaves different:
$ ./yarv -v
ruby 1.9.0 (2005-11-18) [i686-linux]
YARVCore 0.3.3 (rev: 319) [opts: ]
$ ./yarv tst.rb
[1, 2]
[1, 2]
[1, 2]
[1, 2]
[[1, 2], nil]
[1, 2]
[1, 2]
[1, 2]
[1, 2]
[1, 2]
[[1, 2], nil]
[1, 2]
Dominik