From: "Steven G. Harms" Date: 2008-02-17T23:04:57+09:00 Subject: Re: Alter String base class to perform new (private methods) before returning itself when called by print statements Robert, Thank you for such a connsidered reply. 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 just changed my 'slug' type and failed to understand that the preview was for admin-eyes-only. > > In 'active_present" 6 strings are created and returned in an array. > > Each of these strings are passed to a "check_macron" routine which > > removes macrons where needed. > > Personally I find this naming a bit unfortunate: checking is usually a > read only operation while you are actually manipulating something. I see your point on check_macron not being optimally named, let me keep the old name for purposes of this thread but I'll act on that recommendation. > 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 ). > Another alternative (modifying assignment) could be this: > > class LatinVocabulary >    def initialize >      @vars = {} >    end > >    def method_missing(sym, *args, &b) >      name = sym.to_s > >      case >      when /^(.*)=$/ =~ name && args.length == 1 >        # assignment >        @vars[$1] = remove_macron(args.first) >      when args.empty? && @vars.has_key? name >        # getter >        @vars[name] >      else >        super >      end >    end > >    def remove_macron(str) >      ... >    end > end > > And then > > voc = LatinVocabulary.new > voc.die = "alea" > voc.peasant = "agricola" 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. > > instead of the very short and pleasant: > > > aRegularString = "razzle" > > > Perhaps I am mistaken in this? > > No, you are not.  How should the interpreter know that "..." suddenly > creates a LatinString and not a String?  This is really hard coded into > the language.  And it's good that way because otherwise all sorts of > nasty things could happen if anybody could change this. Heh, well I suppose I was a bit overwhelmed by the idea of open classes and figured that Ruby would let me saw my hand off on this point, if I wanted to. That said, I agree it's probably a good thing that this can't get mucked about with. > 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, Steven