From: Logan Capaldo Date: 2006-06-29T05:38:43+09:00 Subject: Re: eigenclass not extensible ? On Jun 28, 2006, at 1:13 PM, Jonas Pfenniger wrote: > ... in the particular case where the method already exists. > > == Example > > class Cls; end > class Cls2; end > > def Cls.somemethod > "defined in Cls" > end > > Cls.somemethod #=> "defined in Cls" > > module Mod > def somemethod > "defined in Mod" > end > end > > Cls.extend Mod > Cls2.extend Mod > > Cls.somemethod #=> "defined in Cls" > Cls2.somemethod #=> "defined in Mod" > > > Why doesn't Cls.somemethod gets overrided ? > > -- > Cheers, > zimbatm > > http://zimbatm.oree.ch > Modules are inserted into the inheritance chain. So if you do something like: class A def x "x" end end module X def x "y" end end class A include X end a = A.new a.x (Look for #x in my singleton class, can't find it? Ok, look for it in A, aha! found it! return "x") class B include X end b = B.new b.x (Look for #x in my singleton class, can't find it? Ok, look for it in B, can't find it? Ok, look for it in X, aha! found it return "y") As you can see, it doesn't really have anything to do with the fact that it's a singleton class, it's just how modules work. The reason why using #extend can override something declared in an objects class is because the singleton class is earlier in the inheritance chain than the actual class of the object. extend includes the module into the singleton class which puts it earlier than the object's real class.