Both lambda and proc work
as well, incidentally. Actually, from what I’ve read they’re identical
in 1.8 (which is what I’m using), but not in 1.9,
Not exactly.
-
lambda and Proc are different to each other in both ruby1.8 and
ruby1.9.
Also, they are different in the same way -
in ruby1.8, proc {} is a synonym for lambda {}
-
in ruby1.9, proc {} is a synonym for Proc.new {}
So to play it safe, never use proc {} - always use lambda {} or Proc.new
{}
For a detailled discussion of the differences, see
http://innig.net/software/ruby/closures-in-ruby.rb
(although I disagree with his rant: these inconsistencies are carefully
considered to make Ruby “do the right thing” in various scenarios)
If you are using closures in the way discussed in this thread, then I
think
lambda {} is probably the best choice as you get a true closure which is
fully independent of the thread of execution where it was defined.
Regards,
Brian.