From: Gavin Sinclair Date: 2004-12-03T14:37:22+09:00 Subject: Re: nested defs, what if... On Friday, December 3, 2004, 4:18:38 PM, nobu wrote: > Hi, > At Fri, 3 Dec 2004 14:00:44 +0900, > Gavin Sinclair wrote in [ruby-talk:122305]: >> > class A >> > def a(y) >> > def b(x) >> > x + y # `y' in a or `undefined local variable'? >> > end >> > 10.times{ |i| print b(i) } >> > end >> > end >> >> Is there any difference between an "inner method" (with 'y' in a) and >> a lambda? > Actually, no. Just a syntax sugar, and it would be implemented > using a lambda. And another question; should b be visible from > other methods called from a? I don't think so. I can't imagine a use case, and the _appearance_ of the code is that b() is private to a(). >> I've happily used lambdas to define private functionality before, but >> I'm concerned about efficiency: >> >> class A >> def a(y) >> b = lambda { |x| x + y } >> 10.times { |i| print b(i) } >> end >> end >> >> Does the lambda attached to b get regenerated with each call to a? > I guess so. So an inner method, which _looks_ static, would actually be inefficiently regenerated each time? (You said the inner method would be syntax sugar for a lambda.) Gavin