From: "David A. Black" Date: 2009-08-15T03:25:45+09:00 Subject: Re: Class method aliased in superclass bypasses subclass overrides Hi -- On Fri, 14 Aug 2009, Marcos wrote: > This seems like it should work: > > class Parent > class << self > def real_method() "Parent" end > alias :fake_method :real_method You don't have to use symbols there; you can do: alias fake_method real_method because alias is a keyword, not a method, so it can take "raw" identifiers. > end > end > > class Child < Parent > def real_method() "Child" end You mean def self.real_method, I think. > end > > But it doesn't: > > Child.fake_method # ==> returns "Parent" instead of "Child" > > If I replace the alias with a real method definition, e.g. > > def fake_method() real_method end > > then it works the way I expect (Child.fake_method returns "Child"). > So what is it about "alias" that bypasses the override? Do I have to > replace my aliases with extra wrapper methods to get the behavior I'm > looking for? The aliasing just means that there's now a method called Parent.fake_method. It doesn't add any methods to any class other than the class where it's executed (in this case, the singleton class of Parent). Basically, creating an alias has the same footprint as writing a method definition. David -- David A. Black / Ruby Power and Light, LLC / http://www.rubypal.com Q: What's the best way to get a really solid knowledge of Ruby? A: Come to our Ruby training in Edison, New Jersey, September 14-17! Instructors: David A. Black and Erik Kastner More info and registration: http://rubyurl.com/vmzN