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?
Best Answer
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 toself
and the instance variables ofself
.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 outerdef
is run.