Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use lambda, when to use Proc.new?

Tags:

ruby

lambda

proc

In Ruby 1.8, there are subtle differences between proc/lambda on the one hand, and Proc.new on the other.

  • What are those differences?
  • Can you give guidelines on how to decide which one to choose?
  • In Ruby 1.9, proc and lambda are different. What's the deal?
like image 722
Michiel de Mare Avatar asked Aug 03 '08 06:08

Michiel de Mare


People also ask

What is difference between lambda and proc?

In Ruby, a lambda is an object similar to a proc. Unlike a proc, a lambda requires a specific number of arguments passed to it, and it return s to its calling method rather than returning immediately. proc_demo = Proc. new { return "Only I print!" }

What is Proc new?

Creates a new Proc object, bound to the current context. Proc::new may be called without a block only within a method with an attached block, in which case that block is converted to the Proc object. def proc_from Proc. new end proc = proc_from { "hello" } proc.

How is a block different from a proc?

Procs are objects, blocks are notA proc (notice the lowercase p) is an instance of the Proc class. This lets us call methods on it and assign it to variables. Procs can also return themselves. In contrast, a block is just part of the syntax of a method call.

What is lambda in Ruby?

Lambda provides runtimes for Ruby that run your code to process events.


2 Answers

Another important but subtle difference between procs created with lambda and procs created with Proc.new is how they handle the return statement:

  • In a lambda-created proc, the return statement returns only from the proc itself
  • In a Proc.new-created proc, the return statement is a little more surprising: it returns control not just from the proc, but also from the method enclosing the proc!

Here's lambda-created proc's return in action. It behaves in a way that you probably expect:

def whowouldwin    mylambda = lambda {return "Freddy"}   mylambda.call    # mylambda gets called and returns "Freddy", and execution   # continues on the next line    return "Jason"  end   whowouldwin #=> "Jason" 

Now here's a Proc.new-created proc's return doing the same thing. You're about to see one of those cases where Ruby breaks the much-vaunted Principle of Least Surprise:

def whowouldwin2    myproc = Proc.new {return "Freddy"}   myproc.call    # myproc gets called and returns "Freddy",    # but also returns control from whowhouldwin2!   # The line below *never* gets executed.    return "Jason"  end   whowouldwin2          #=> "Freddy" 

Thanks to this surprising behavior (as well as less typing), I tend to favor using lambda over Proc.new when making procs.

like image 77
Joey deVilla Avatar answered Oct 19 '22 06:10

Joey deVilla


To provide further clarification:

Joey says that the return behavior of Proc.new is surprising. However when you consider that Proc.new behaves like a block this is not surprising as that is exactly how blocks behave. lambas on the other hand behave more like methods.

This actually explains why Procs are flexible when it comes to arity (number of arguments) whereas lambdas are not. Blocks don't require all their arguments to be provided but methods do (unless a default is provided). While providing lambda argument default is not an option in Ruby 1.8, it is now supported in Ruby 1.9 with the alternative lambda syntax (as noted by webmat):

concat = ->(a, b=2){ "#{a}#{b}" } concat.call(4,5) # => "45" concat.call(1)   # => "12" 

And Michiel de Mare (the OP) is incorrect about the Procs and lambda behaving the same with arity in Ruby 1.9. I have verified that they still maintain the behavior from 1.8 as specified above.

break statements don't actually make much sense in either Procs or lambdas. In Procs, the break would return you from Proc.new which has already been completed. And it doesn't make any sense to break from a lambda since it's essentially a method, and you would never break from the top level of a method.

next, redo, and raise behave the same in both Procs and lambdas. Whereas retry is not allowed in either and will raise an exception.

And finally, the proc method should never be used as it is inconsistent and has unexpected behavior. In Ruby 1.8 it actually returns a lambda! In Ruby 1.9 this has been fixed and it returns a Proc. If you want to create a Proc, stick with Proc.new.

For more information, I highly recommend O'Reilly's The Ruby Programming Language which is my source for most of this information.

like image 33
Peter Wagenet Avatar answered Oct 19 '22 05:10

Peter Wagenet