In my latest project, I was a bit surprised by the way arguments to
Proc.new were handled. Somehow an array which i passed in was
mysteriously lost and presented as a nil within the block I had
created.
If this is to be expected, I would have liked to see a mention of it
in the documentation. After reading the documentation more carefully,
I realized that if i would use Kernel.proc instead of Proc.new it
should do the same "except the resulting Proc objects check the number
of parameters passed", which evidentially in my case also means that
the parameter actually goes through.
Wondering whether this was a bug or not, I tried out the latest CVS
version to see whether the behavior had changed or not. Indeed the
behavior was changed in the CVS version, but in a direction quite
opposite to what I had hoped for, since now both Proc.new and
Kernel.proc performs in the same way. (Eating up my array argument)
Kernel.lambda seems to work the way I want for all cases though and
thus using this will solve my problems, but I was curious whether this
difference between lambda and proc is intended, and also whether the
change of semantics from current to CVS was intended.
Below is an example which isn't especially meaningful, but serves it's
purpose to highlight the differences between the semantics of the
different approaches in different versions of ruby.
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, []]
Is this to be expected?
!g