Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
    
I don't know ruby or what a "proc" is, but often times in languages a lambda's purpose is to act more as a variable (closure) than as a method, and can be passed around as such. Can a plain function in ruby be passed around like a variable? –  Jimmy Hoffa Oct 3 '12 at 16:15
    
@JimmyHoffa In ruby you can get a Method-object with Object#method. The Method object acts as a closure in obj’s object instance, so instance variables and the value of self remain available. –  knut Oct 3 '12 at 17:45
    
@knut How is that implemented though? Does it return a Method-object as its own object, or as a proc? –  KChaloux Oct 3 '12 at 17:51
    
@KChaloux It's an own Method-object. The Method-class is inheriting from Proc. But I'm no expert for this, that's why I am curious on other answers for this question ;) –  knut Oct 3 '12 at 17:57
    
You can make currying with lambdas and get another lambda, ie composition of functions. –  user66847 Oct 3 '12 at 22:20
show 1 more comment

2 Answers

up vote 3 down vote accepted

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.