From: david karapetyan Date: 2007-06-05T02:58:52+09:00 Subject: Re: code problems ------=_Part_4291_23696693.1180979919769 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline cool and thanks for the help On 6/3/07, Nobuyoshi Nakada wrote: > > Hi, > > At Mon, 4 Jun 2007 13:25:50 +0900, > david karapetyan wrote in [ruby-talk:254219]: > > thanks. i didn't realize i had to provide the block as well when i was > > creating the closure. i was hoping for a closure that could take a > different > > block every time. i wanted fib to be a function that had access to a and > b > > and every call to fib would require a block. as it is fib has its block > > fixed at creation time. do you know of a way of doing what i wanted to > do > > originally? > > You need ruby 1.9. Block passing to a block isn't supported in > 1.8. > > def fibber > a = 1 > b = 1 > lambda do |&block| > block.call(a) > a,b = a+b,a > end > end > > irb(main):010:0> fib = fibber > => # > irb(main):011:0> 10.times {fib.call {|x|p x}} > 1 > 2 > 3 > 5 > 8 > 13 > 21 > 34 > 55 > 89 > => 10 > > -- > Nobu Nakada > > ------=_Part_4291_23696693.1180979919769--