From: Robert Klemme Date: 2007-05-31T17:55:27+09:00 Subject: Re: Underscore On 30.05.2007 22:36, Robert Dober wrote: > On 5/30/07, Logan Capaldo wrote: >> On 5/30/07, Robert Dober wrote: >> > >> > On 5/30/07, Brian Candler wrote: >> > > On Wed, May 30, 2007 at 07:50:05AM +0900, Logan Capaldo wrote: >> > > > On 5/29/07, Robert Klemme wrote: >> > > > > >> > > > > >> > > > >I was too fast (again). Even though the return value is used, I'd >> > rather >> > > > >do >> > > > > >> > > > >n.times { x = yield x } >> > > > > >> > > > >than using #inject which does more than needed in this case. :-) >> > > > > >> > > > >Now, did I look at all aspects...? >> > > > >> > > > >> > > > Almost. >> > > > you'd actually need >> > > > n.times { x = yield x } >> > > > x >> > > >> > > Almost almost :-) >> > > >> > > x = nil >> > > n.times { x = yield x } >> > > x >> > > >> > > >> > Robert was very quick indeed, he did not repeat the context of the >> > expression which was >> > >> > def forgot_the_name(x,...) >> > n.times{ x = yield x } >> >> >> Still need x here >> >> end >> > >> > Now it works ;) >> >> >> No it doesn't ;) n.times { ... } returns n. > Obviously I am too stupid to test, sorry, I did test it, who knows how ;) > Seriously I do know now, n just was the expected result of the block :( > > def anyhow(count, x) > count.times{x=yield x} > x > end > > Maybe we can still agree that this is better than inspect ;) > And yes thanks for the Ruby classes I got. Hm... Thinking a bit more about this I am not sure I'd stick with my proposal. I think I'd probably do irb(main):001:0> require 'enumerator' => true irb(main):002:0> def rep(n,init=nil) n.to_enum(:times).inject(init) {|x,| yield x} end => nil irb(main):003:0> rep(5,"x") {|s| puts s; s<<"."} x x. x.. x... x.... => "x....." Hmm... Kind regards robert