now try this on for size. I'm sure you know how this works:
> def a
> yield
> end
=> nil
> a { 42 }
=> 42
But did you know this? > def b
> Proc.new.call
> end
=> nil
> b { 42 }
=> 42
Proc.new without any arguments works the same as yield. tripped me out when I found this the other day.So what is the point of using Proc instead of lambda then?
I can't see a case where the behavior of proc is the desired behavior (although now that with the difference in my mind I might end up using it one day...)
So this works:
But this does not: If you do something like save the proc for later, you are in trouble: Because the method call where the proc was defined no longer exists. So Ruby closes over local variable bindings, saving them for later, but not over control flow.If it did, the return keyword would end up acting like a continuation.