From: "trans. (T. Onoma)" Date: 2004-12-03T20:57:24+09:00 Subject: Re: nested defs, what if... On Friday 03 December 2004 04:00 am, Mauricio Fern叩ndez wrote: | On Fri, Dec 03, 2004 at 03:02:49PM +0900, trans. (T. Onoma) wrote: | > Yes, I think so since lambdas are not singleton/multiton (could they be?) | > Of course the fix is something like: | > | > class A | > def a(y) | > @a_b ||= lambda { |x| x + y } | > 10.times { |i| print @a_b(i) } | > end | > end | | Not quite... | >> class A; def a(y); @a_b ||= lambda { |x| x + y }; 10.times { |i| | >> print @a_b[i]}; end end | => nil | >> a = A.new | => # | >> a.a(10) | 10111213141516171819=> 10 | >> a.a(1000) | 10111213141516171819=> 10 Ah right. There's a gotchya. Try, class A def a(y) @a_b ||= lambda { |x,y| x + y } 10.times { |i| print @a_b(i,y) } end end I think this is another reason for having the inner defs with a protected scope. | > But true inner methods would be the bomb for these use cases (IMHO). | | They don't seem any more powerful than lambdas, just sugar. | They would offer some opportunities for optimization though. Depends on the meaning of "powerful". But see above. Scope isolation can be advantageous. And, yes, optimization would be nice too. T.