I’ve been working in Ruby for the last couple weeks, and I’ve come to the subject of procs, lambdas and blocks. After reading a fair share of examples from a variety of sources, I don’t how they’re much different from small, specialized functions. It’s entirely possible that the examples I’ve read aren’t showing the power behind procs and lambdas.
def zero_function(x)
x = x.to_s
if x.length == 1
return x = "0" + x
else
return x
end
end
zero_lambda = lambda {|x|
x = x.to_s
if x.length == 1
return x = "0" + x
else
return x
end
}
zero_proc = Proc.new {|x|
x = x.to_s
if x.length == 1
puts x = "0" + x
else
puts x
end
}
puts zero_function(4)
puts zero_lambda.call(3)
zero_proc.call(2)
This function, proc, and lambda do the exact same thing, just slightly different. Is there any reason to choose one over another?
6
First of all, what do you mean by “functions”? Proc and lambda are “functions” — and they evaluate to a function object, which could be assigned to a variable or used in any other expression. There is no other way to define a non-method “function” in Ruby.
def
, by contrast, defines a method in the current module (you are always inside some module, either explicitly or implicitly). A method is obviously different from a bare function because a method implicitly has access to self
and the instance variables of self
.
More importantly, Proc and lambda are closures — they capture local variables from the enclosing scope where they were created, and you can use those variables inside the closure. This allows function objects to hold state.
By contrast, defining a method inside another method definition will not give access to the local variables in the outer method body. So it’s the same as if you moved the inner definition outside, except that the method defined by the inner def
will only exist after the method defined by the outer def
is run.
Good question! I’m curious on the answers.
One difference I know:
You get no error with this call:
zero_proc.call()
But you get a wrong number of arguments (0 for 1) (ArgumentError)
-error with the following calls:
puts zero_function()
puts zero_lambda.call()
Methods and lambdas check the number of parameters, Proc-objects don’t, the use nil
if there are missing parameters.
The other big difference between procs, methods and lambdas is how they deal with break and return.
Basically, a return in a Proc will jump out of any enclosing method as well, and a return from a lambda will only exit the lambda.
A break from a Proc tries to break out of an enclosing iteration (if there’s no iteration, you’ll get an error), while a break from a lambda is just like a return.
More details here: http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them/