From: Robert Klemme Date: 2008-02-18T01:24:55+09:00 Subject: Re: Alter String base class to perform new (private methods) before returning itself when called by print statements On 17.02.2008 15:00, Steven G. Harms wrote: > Thank you for such a connsidered reply. You're welcome! > On Feb 17, 4:57 am, Robert Klemme wrote: >> :-) My Latin is very rusty nowadays but this brings back memories (our >> first sentence in class five was "agricola arat")... :-) > > It's always about the farmers, isn't it? :-)) >>> Here's the output (at top) and code (at bottom):http://stevengharms.com/?page_id=1159 >> Where exactly? I could neither see code nor latin words on that page. > > I *believe* you can now see the code at: > http://stevengharms.com/improving-the-latin-command-line-verb-conjugator I can confirm that. :-) Few remarks: your type checking can be simplified: case input when Array ... when String ... else raise ArgumentError, "wrong type: #{input.inspect}" end Same for the conjugation, for which I would introduce symbols as names (not "1", "2" etc.). Same in evaluate_conjugation. def evaluate_conjugation case @infinitive when /\?re$/ # what exactly should your regexp look like? # question mark without escaping backslash # is not so good "1" # or rather a symbol denoting the name when /... ... else end end >> I can see where your motivation comes from. Generally I'd opt for not >> putting this into class String because it is too specialized (i.e >> functionality that does make sense in context of your application only). > [ code snip ] > > That's sensible. I've never seen this Struct.new syntax so I'll be > eager to try it out. I think i ( perhaps cumbersomely? ) wind up > going more-or-less along this design pattern ( now that you can see > the code ). Struct.new is actually just a convenient way to create a new class with a set of properties that are honored during #hash, #eql? and #== so instances can be easily compared and used as Hash keys. [...] > This feels very "intro to metaprogramming" - the use of the > method_missing method could, in principle, be used to expand the > class. i.e.(pseudocode) "if method_missing (:active_present) do the > active_present build with macron_sanitization routine". This may be > where my code goes to once I get a bit more cleaned up. If you know the method beforehand it is more efficient to define it right away. >> LatinStrings to be returned, i.e. does the result of this method call >> have to be modified already or do you need to be able to do it later? >> If the former, then you can apply the conversion in Verb#active_present, >> if the latter you can return a LatinString (as shown above). > > I think I wind up doing the former > > Thank you for your illuminating replies, You're welcome! I'm glad I could help. Kind regards robert