From: dagbrown@... (Dave Brown) Date: 2003-12-27T14:41:50+09:00 Subject: Re: Closures and Letrec In article , Charles Comstock wrote: : Is there a way to create recursive anonymous closures in ruby? For instance : if I write : even = proc {|x| x == 0 ? true : odd(x-1)} : odd = proc {|x| x == 1 ? true : even(x-1)} : : In this case even definitely doesn't know about odd, and actually doesn't even : know about even. Odd is aware of even though. Sure, you just have to remember about The Miracle Of Duck Typing, and make sure that both your variables are visible to each other. Since a proc is a method, you can call it. Since an object is an object, you can send messages to it. One such message is [] (which is an alias for Proc#call). Also, I found a bug in your code. :-) $even = proc { |x| case x when 0 then true when 1 then false else $odd[x-1] end } $odd = proc { |x| case x when 0 then false when 1 then true else $even[x-1] end } p $odd[3] p $even[4] p $odd[4] p $even[3] $odd isn't even defined when $even is set, but that doesn't stop Ruby: it doesn't worry about things like that until you're actually trying to call methods. By the time it actually gets around to trying to call methods against odd, odd is defined and you can call [] with no problems. --Dave