From: Ross Bamford Date: 2006-01-09T10:53:02+09:00 Subject: Re: Iterator Fu Failing Me On Mon, 09 Jan 2006 01:09:23 -0000, James Edward Gray II wrote: > On Jan 8, 2006, at 6:58 PM, jwesley wrote: > >> Perhaps I'm missing the point, but wouldn't the following work: (?) >> >> elements = tokens.map do |token| >> val = nil >> [ClassA, ClassB, ClassC].find { |kind| val = kind.parse?(token) } >> val >> end > > It works just fine, yes. Just doesn't feel very Rubyish to me. > Not sure this is more Rubyish (seems a bit line-noisy) but: class Parser class << self def parse?(tok) tok.downcase if tok == self.name end end end class One < Parser; end class Two < Parser; end class Three < Parser; end parsers = [One, Two, Three] tokens = ['One','Two','One','siz','Four','Two'] r = tokens.inject [] do |r,token| r << (parsers.zip([token] * parsers.length).detect { |p,tok| p.parse? tok } || [])[0] end p r Seems to work. It's *terribly* inefficient though I guess. -- Ross Bamford - rosco@roscopeco.remove.co.uk