From: "Douglas A. Seifert" Date: 2007-11-14T12:16:17+09:00 Subject: Re: recursion with blocks --------------030802060305090100020003 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Pat Maddox wrote: > On Nov 13, 2007 4:15 PM, James Edward Gray II wrote: > >> On Nov 13, 2007, at 6:01 PM, Mike Perham wrote: >> >> >>> You aren't actually recursing inside the block, >>> just using the block as a conditional. I was trying to figure out how >>> to recurse inside the block. >>> >> Yeah, I was just using the block inside a recursive method. You are >> right. >> >> Can you elaborate on what you mean by "recurse inside a block?" I'm >> having trouble thinking of an example. >> >> James Edward Gray II >> >> >> > > I think he means something like this? > > fact = lambda {|val| return 1 if val <= 0; val * fact[val - 1] } > fact[4] => 24 > > but I'm not sure... > > Pat > > This works: irb(main):001:0> fact = lambda {|val| val <= 0 ? 1 : val * fact[val-1] } => # irb(main):002:0> fact.call(4) => 24 irb(main):003:0> fact.call(6) => 720 --------------030802060305090100020003--