From: Ross Bamford Date: 2006-01-09T11:23:01+09:00 Subject: Re: Iterator Fu Failing Me On Mon, 09 Jan 2006 01:45:11 -0000, Ross Bamford wrote: > 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: > This one looks worse, but at least really does seem to work this time: r = tokens.inject([]) do |arr,token| arr << parsers.zip([token]*parsers.length).inject(nil) do |out,pt| out or pt[0].parse?(pt[1]) end end slotted into the code before and it gives: ["one", "two", "one", nil, nil, "three"] Sorry about that. -- Ross Bamford - rosco@roscopeco.remove.co.uk