What's Up, Proc?
April 3, 2015
There's a method to my madness. Ok, so that was really awful, but this week's technical blog will be discussing Blocks, Procs, and Lambdas. It feel's like a bit of madness on the surface, to be honest. Whenever a method is called, it will execute what ever it contains. This would be the block. It's the same for any loop, after the initial condition, the loop will run...something. The cool thing about Ruby is that it doesn't execute the block on sight. Instead, it remembers the commands given and executes at the defined moment.
So, that brings us to the differences between a Lambda and a Proc. Both are similar to a nameless method. You can call a proc or lambda without having to name either one. They will both execute a specfied block. There are two significant differences though. A Proc will return whatever follows it in the block immeadiately in the processing of the method, while a lambda will finish the code and return in the main body of the method. See the following,
def func_one
proc_new = Proc.new {return "123"}
proc_new.call
return "456"
end
def func_two
lambda_new = lambda {return "123"}
lambda_new.call
return "456"
end
func_one will retun "123" while func_two returns "456". The other difference concerns arguments. A Lambda will raise an Argument Error if given an extra argument, while a Proc will accept any number of arguments. I think I understand all of this, but we'll see how it goes.
Again Soon,
Staunton