From: Dave Thomas Date: 2001-07-08T23:58:56+09:00 Subject: [ruby-talk:17500] Re: Aliases for class methods "HarryO" writes: > I had a quick skim through chapter 19 and it's now as clear as mud > :-). Yeah, me too :) > Can you explain why just putting the "alias original open" inside the > "class File ... end" doesn't have the desired effect? I would have > thought that anything within that scope would be effectively the same as > putting it inside "class << self ... end". Let's look at another example class File alias original path def path ... end end f = File.new(...) puts f.path Here we've defined an alias for the _instance_ method 'path'. These instance methods are defined without a prefix in the class body, and are invoked by sending their name to instances of class File ('f' in this case). Class methods are different: class File def File.wombat end end Here, the receiver isn't an object of class File, it's class File itself: puts File.wombat So these class methods must be stored in a different place to the instance methods. When you send an object of class File a message, it looks for methods in class File. When you send _class File_ a message, it has to look somewhere else. It turns out that the place it looks is in File's metaclass, which is the class of class File. (!) So, aliasing works on instance methods, and the class methods of file are instance methods of File's metaclass, so... we have to get ourselves into the metaclass's context before using alias if we want to alias File's class methods. When you say 'class <