On Fri, Sep 06, 2002 at 02:08:19AM +0900, Tobias Peters wrote:
> This just came to my mind:  Since ruby allows for nested singleton 
> methods, you could probably write:
> 
>   def sqrt(x)
>     o = Object.new
>     def o.good_enough(g1,g2)
>       ((g1 - g2).abs / g2) < .001
>     end
>     def o.improve(guess)
>       average(guess, x/guess)
>     end
>     def o.sqrt_iter(g1,g2)
>       good_enough(g1,g2) ? g2 : sqrt_iter(g2, improve(g2))
>     end
>     o.sqrt_iter(1.0, 2.0)
>   end

Most defs within defs can also be resolved using procs.

def sqrt(x)
  good_enough = proc { |g1,g2| ((g1 - g2).abs / g2) < .001 }
  improve     = proc { |guess| average(guess, x/guess) }
  sqrt_iter   = proc { |g1,g2| good_enough[g1,g2] ? g2 : sqrt_iter[g2, improve[g2]] }
  sqrt_iter[1.0, 2.0]
end

-- 
Alan Chen
Digikata LLC
http://digikata.com