From: Bob Hutchison Date: 2006-01-08T23:05:22+09:00 Subject: Re: Iterator Fu Failing Me On Jan 7, 2006, at 5:43 PM, Robert Klemme wrote: > 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 Personally, I don't like the break. It'd be very nice if there were equivalent operations returning the result of the calculation rather than the object. > > Thanks for correcting me! Sorry for the brutally finger-tied attempts at communication :-) > > robert > > ---- Bob Hutchison -- blogs at Recursive Design Inc. -- Raconteur -- xampl for Ruby --