Question
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?
Answer
Another important but subtle difference is in the way procs created with lambda and procs created with Proc.new handle the return statement:
- In a
lambda-created proc, thereturnstatement returns only from the proc itself - In a
Proc.new-created proc, thereturnstatement 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 behaviour (as well as less typing), I tend to favour using lambda over Proc.new when making procs.
0 comments:
Post a Comment