Tobias Peters wrote:
> On Fri, 30 Aug 2002, Gavin Sinclair wrote:
>
>> > The obvious OO way seems to be that each subclass would, on loading,
>> > register itself with LogLine.
> 
> Sounds sensible. Just add to your logline class:
> class LogLine
>   @@subclasses = [LogLine]
>   def LogLine.inherited(subclass)
>     @@subclasses << subclass
>   end
>   def LogLine.create(logline_string)
>     # parse the date
>     ratings = @@subclasses.collect{|subclass|
>       subclass.rate(string_after_date)
>     }
>     best_subclass = @@subclasses[ratings.index(ratings.max)]
>     best_subclass.new(date, string_after_date)
>   end
> end

Awesome! The Class.inherited method is just what I needed. Sorry I didn't 
notice that the first time I read the docs!

Regarding the factory method, would something like this be just as 
Ruby-appropriate?

    def LogLine.create(logline_string)
        base_line = LogLine.new(logline_string)
        ratings = @@subclasses.collect{|subclass|
           subclass.rate(base_line)
        }
        best_subclass = @@subclasses[ratings.index(ratings.max)]
        best_subclass.new(base_line)
    end
 
My notion is that rather than reparsing string_after_date over and over, the 
common features would be parsed exactly once in LogLine.initialize, and the 
subclasses would only parse the log message content. (Further progress 
along this path could be made by LogLine only getting ratings from direct 
subclasses, letting the subsubclasses poll their children as needed, but 
that's more an implementation detail.)

LogLine.new would end up being polymorphic, of course. Speaking of which, 
what's the best Ruby idiom for that? In Java, one does this as something 
like

    LogLine(String logString) {
        parse(logString);
    }

    LogLine(LogLine logLine) {
        copy(logLine);
    }

Is the best way to do that in Ruby a case statement in the 
LogLine.initialize method that checks the type of the argument?


Thanks,

William