From: Mike Gold Date: 2009-01-13T12:07:28+09:00 Subject: Re: functional programming Brian Candler wrote: > Mike Gold wrote: >> Brian Candler wrote: >>> >>>Yes, a variable holding a lambda is a different beast to a method. As >>>you said, methods are associated with objects, and lambdas are not. >>>Usually one suits the usage case better than the other. >>> >>>I find the distinct behaviours of both to be very useful in practice. >> >> Can you give an example of where the difference is "very useful"? > > Well, as you know, blocks are roughly just in-line lambdas: > > 10.times { |x| puts x } > If by "roughly" you mean "not", then yes. Blocks are not lambdas, they are Procs. > I was thinking of the following properties of the method/lambda > difference: > > (1) You can return directly from a method, even from inside blocks. > > class Foo > def bar > 10.times { |x| ... > 10.times { |y| ... > return "foo" if baz(x,y) > } > } > end > end Lambdas do not abort the enclosing method with 'return'. def lambda_test func = lambda { return } puts "before" func.call puts "after" end def proc_test func = Proc.new { return } puts "before" func.call puts "after" end lambda_test # => before # after proc_test # => before The topic was about the problems arising from the syntactic difference between methods and lambdas. You've changed gears to a generic discussion about procs. My question is yet unaddressed: why is func.call() or func[] or func.() "very useful" compared to func()? As I mentioned, in every case I've found it to be a hassle, for the reasons previously stated. -- Posted via http://www.ruby-forum.com/.