On 29 Aug 2001, at 12:19, Alexander Schofield wrote:

> Nat Pryce <nat.pryce / b13media.com> wrote:
> > factorial = proc do |x|
> >     (x == 1) ? 1 : (x * factorial.call( x-1 ))
> > end
> 
> That certainly works, and serves to show why a truly anonymous
> recursive Proc would be a small improvement at best, but it would
> still be nice to have one with true anonymity (in both scopes)

I hoped to find a way to write something like

rp = refproc { |n| n>1 ? n * selfproc[n-1] : 1 }

but I couldn't get the identifier "selfproc" into the block. The 
construct I came up with is not as nice, but as an advantage you 
can choose the name you want to use for the recursive Proc:

rp = ref { |sp| proc { |n| n>1 ? n * sp[n-1] : 1 } }
p rp[5]

It uses a form of Jim's Y combinator:

def ref( &orgproc )
  proc { |f| f[f] } [ proc { |f| orgproc[ proc { |*x| f[f][*x] } ] } ]
end

This seems to work with other argument lists as well.

Pit