From: Michael Fellinger Date: 2010-08-27T14:27:37+09:00 Subject: Re: Get Proc of current block? On Fri, Aug 27, 2010 at 10:42 AM, Intransition wrote: > Is there any way to get a hold of the current block? I've been playing > around with recursive enumerations and it occurs to me that the most > flexible technique would simply be to reuse the currently running > block. > > For example, where `this` would be the current block: > >  h = {:a=>1, :b=>{:c=>3}} > >  h.map{ |k,v| Hash===v ? [k.to_s, v.map(&this)] : [k.to_s,v.to_s] } > >  #=> {"a"=>"1", "b"=>{"c"=>"3"}} That's called the Y-combinator, something i wish could be added to core, but would be afraid of the many nay-sayers who are already confused by the current number of ways of creating blocks :) def y(*args, &block) y = lambda{|*args| block.(*args, &y) } end p y{|n, acc, &b| n < 2 ? acc : b.(n-1, n * acc) }.(5, 1) -- Michael Fellinger CTO, The Rubyists, LLC