From: Robert Klemme Date: 2005-09-19T19:46:39+09:00 Subject: Re: Reference to 'this block' Mauricio Fern�ndez wrote: > On Mon, Sep 19, 2005 at 06:23:23PM +0900, Brian Candler wrote: >> I want the object to be self-contained. 'self' doesn't work. Best I >> can come up with is to try and hide it: >> >> a = __myfoo = lambda { |x| x < 2 ? 1 : x * __myfoo.call(x-1) } > > You can use the Y combinator or something as simple as > > # make sure _l is block-local > a = lambda{ _l = lambda{|x| x < 2 ? 1 : x * _l[x-1]}}[] > p a[5] > _l = 1 # doesn't matter > p a[5] If you want to be sure use a function: >> def t() f = lambda {|x| x < 2 ? 1 : x * f[x - 1]} end => nil >> t[10] => 3628800 Btw, recursions don't perform well with Ruby so an iterative solution is usually better. Kind regards robert