4 ms·
Wrong. Ruby code blocks can be Methods, Procs or Lambdas. Methods are the hard-coded blocks of code on a Class or a Module (or in the global space, which actual
by jameskilton 17y ago
Wrong. Ruby code blocks can be Methods, Procs or Lambdas. Methods are the hard-coded blocks of code on a Class or a Module (or in the global space, which actually is in the Kernel module). A "block" is simply a contained/scoped bit of code.
Procs and Lambdas exist because they exhibit different scoping rules. If you want local scoping only, use a lamdba. If you want access to information outside the block, use a proc.
Of course, your rant sure sounds like you didn't actually read Katz's blog post, or you wouldn't be using terms like "callable types." The post explains exactly WHY Ruby doesn't have an equivalent __call__ structure...
- blubb 17y ago>> lambda {} == proc {} => true Can you shed some light on the difference? I always thought that lambda is simply a synonym for proc - has this changed in more recent versions?
- carbon8 17y agoSee here: http://www.robertsosinski.com/2008/12/21/understanding-ruby-blocks-procs-and-lambdas/ http://www.robertsosinski.com/2008/12/21/understanding-ruby-... Proc objects created with lambda check arguments and have "diminutive return" so they behave as anonymous methods. Also, Kernel#proc is an alias to Kernel#lambda, so the only way you get the bare Proc behavior is by calling Proc.new directly.
- blubb 17y agoAh, I didn't know that Proc.new != proc/lambda :) They both return Proc objects though, so I wonder if it is possible to "convert" them.
- carbon8 17y agoOne small note, as pointed out in http://innig.net/software/ruby/closures-in-ruby.rb http://innig.net/software/ruby/closures-in-ruby.rb posted by arohner above: although in 1.8 proc is an alias for lambda, in 1.9 it's now an alias for Proc.new.
- FooBarWidget 17y agoThe empty lambda or empty proc is a special object that always points to the same instance. Probably some kind of optimization.