On 2006.01.07 23:49, ara.t.howard / noaa.gov wrote:
> On Sat, 7 Jan 2006, Eero Saynatkari wrote:
> 
> >On 2006.01.07 13:36, James Edward Gray II wrote:
> >>I have a group of classes, all implementing a parse?() class method.
> >>Calling parse?(token) will return the constructed object, if it can
> >>be parsed by this class, or false otherwise.
> >>
> >>I want to run a bunch of tokens through these classes, grabbing the
> >>first fit.  For example:
> >>
> >>  elements = tokens.map do |token|
> >>    ClassA.parse?(token) or
> >>    ClassB.parse?(token) or
> >>    ClassC.parse?(token)
> >>  end
> >>
> >>That works.  Now can anyone give me a version of the middle section
> >>that doesn't require I call parse?() 50 times?  I want something
> >>close to:
> >>
> >>  elements = tokens.map do |token|
> >>    [ClassA, ClassB, ClassC].find { |kind| kind.parse?(token) }
> >>  end
> >
> > # Not tested
> > elements  = tokens.map {|token|
> >               [A, B, C].each {|kind|
> >                 result = kind.parse?(token) and break result
> >               }
> >             }
> 
> fails when no kind parses.  in this case the result will be
> 
>   [[A,B,C]] (the return of each...)

You are (of course) absolutely right, it should be 'return result'
coupled with a default nil/false at the end.

One of these days I will learn not to write mail at four in the
morning :)

> regards.
> -a


E