From: Robert Klemme Date: 2006-01-08T07:43:00+09:00 Subject: Re: Iterator Fu Failing Me Bob Hutchison wrote: > On Jan 7, 2006, at 1:08 PM, Robert Klemme wrote: >>> >>> You mean #detect or #select? >> >> #detect - because it stops as soon as it has a hit while #select >> will return an array. We need just the first hit. >> >>>> [nil,nil,nil,"w",2,3].find {|x|puts x;x} >> nil >> nil >> nil >> w >> => "w" >>>> [nil,nil,nil,"w",2,3].select {|x|puts x;x} >> nil >> nil >> nil >> w >> 2 >> 3 >> => ["w", 2, 3] >> > > The trouble is that detect returns the 'x' not the result of > processing x successfully (or, in terms of the original post, the > class that accepts the token not the result of the #parse? method). > That's why you have to capture the result in the block. Map returns > the result of processing. Aargh! I shoul've tested more carefully. Yes you're completely right. But the inject version still works, I think that's then my favourite. elements = tokens.map do |tok| parsers.inject(false) {|a,pars| a = pars.parse? tok and break a} end Thanks for correcting me! robert