From: dblack@... Date: 2006-12-14T04:50:47+09:00 Subject: Re: How do I catch a missing method on a passed block? Hi -- On Thu, 14 Dec 2006, Giles Bowkett wrote: >> Most of what people need to grasp and embrace, in order to understand >> and use the vast bulk of these techniques, and others, is: >> >> * classes are objects >> * each object (or almost each object) has both a "birth class" and a >> singleton class >> * objects don't have methods; they look for them along a >> class/module search path, with well-defined rules of order and >> inclusion >> * there's one and only one 'self' at every point in the program >> * instance variables belong to 'self', always >> * class variables are completely unrelated to instance variables, >> in spite of the use of @ in one and @@ in the other >> * the def and class keywords start new local scopes >> * the class keyword can take either a constant (for a named class) >> or a "<< object" expression (for a singleton class) > > I have to admit, I don't understand what this means in practical > terms. In fact I have a question I need to find an answer to, and I > want to post it in this thread, rather than as its own thing, in the > possibly idealistic hope that somebody can frame an answer in terms of > these principles, and help me understand them. If they're not of practical value, then they're not of value. So I'll give it a try :-) > Basically, I need to change a class method. It's the Ferret method > full_text_search, in a Rails app. > > e.g.: > > Thing.full_text_search("asdf") > > I need to modify this class method. I tried alias, but did it wrong. > > alias original_full_text_search full_text_search > > but of course it doesn't work, because it's not an instance method but > a class method. Is this a good place for class_eval? I could be way > off. > > Actually now that I re-read it, I think I do understand the basic > concepts, I think I'm just weak on the syntax. In general, if you want to do: alias old_x x you want to do so inside a class definition body for the class where x is defined (or a subclass of that class). A "class method" is basically a singleton method of a Class object. That means that the class where it's defined is the singleton method of that class object (because that's what singleton classes are: the places where objects' singleton methods are defined). In your case, the object in question is Thing. To get into a class definition body for a singleton class, you use the class keyword plus the "<< object" construct: class << Thing You can now perform the aliasing: alias original_full_text_search full_text_search # redefinitions etc. will follow here end Here's a complete mini-example: class Thing def self.x puts "old x" end end class << Thing alias old_x x def x puts "new x..." old_x end end Thing.x David -- Q. What's a good holiday present for the serious Rails developer? A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black) aka The Ruby book for Rails developers! Q. Where can I get Ruby/Rails on-site training, consulting, coaching? A. Ruby Power and Light, LLC (http://www.rubypal.com)