From: Josh Cheek Date: 2013-05-14T14:25:25+09:00 Subject: Re: callback limitations with `inherited` --089e01493c8a1f140604dca6dc61 Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: quoted-printable On Mon, May 13, 2013 at 1:23 PM, Love U Ruby wrote: > class C > def self.inherited(subclass) > puts "#{self} just got subclassed by #{subclass}." > end > end > class D < C > end > > The limits of the inherited callback: > > *Everything has its limits, including the inherited callback. When D > inherits from C, C is D=92s superclass; but in addition, C=92s singleton > class is the superclass of D=92s singleton class. That=92s how D manages = to > be able to call C=92s class methods. But no callback is triggered. Even i= f > you define inherited in C=92s singleton class, it=92s never called.* > > can anyone help me what the lines above inside ** means? > > -- > Posted via http://www.ruby-forum.com/. > So D is sublcassing C. This means that C's instance methods can be called from instances of D, so D's superclass is C. BUT, notice that C's singleton methods can also be called from D. So we can infer that D's singleton class's superclass is C's singleton class: D.singleton_class.superclass =3D=3D C.singleton_class # =3D> true So when we subclass, we can see that there are actually two classes who get their superclass changed. We might expect, then, that if we defined the inherited method on C's singleton class, that it would be called, because D's singleton class subclasses it: class C class << self def self.inherited(subclass) puts "C's singleton class just got subclassed by #{subclass}" end end def self.inherited(subclass) puts "#{self} just got subclassed by #{subclass}." end end However, only C.inherited is called, not C.singleton_class.inherited FWIW, I think it's probably always a bad idea to use hooks like this. I just can't think of anything good you can do with it (see Rails for an example of how even seemingly good uses become abuse). --089e01493c8a1f140604dca6dc61 Content-Type: text/html; charset=windows-1252 Content-Transfer-Encoding: quoted-printable On Mon, May 13, 2013 at 1:23 PM, Love U Ruby <lists@ruby-forum.com= > wrote:
class C
def self.inherited(subclass)
puts "#{self} just got subclassed by #{subclass}."
end
end
class D < C
end

The limits of the inherited callback:

*Everything has its limits, including the inherited callback. When D
inherits from C, C is D=92s superclass; but in addition, C=92s singleton class is the superclass of D=92s singleton class. That=92s how D manages to=
be able to call C=92s class methods. But no callback is triggered. Even if<= br> you define inherited in C=92s singleton class, it=92s never called.*

can anyone help me what the lines above inside ** means?

--
Posted via http://= www.ruby-forum.com/.


So D is sublcassin= g C. This means that C's instance methods can be called from instances = of D, so D's superclass is C.

BUT, notice that= C's singleton methods can also be called from D. So we can infer that = D's singleton class's superclass is C's singleton class:

D.singleton_class.superclass =3D=3D C.singleton_class = =A0# =3D> true

So when we subclass, we can see = that there are actually two classes who get their superclass changed. We mi= ght expect, then, that if we defined the inherited method on C's single= ton class, that it would be called, because D's singleton class subclas= ses it:

class C
=A0 class << self
=A0 =A0 def self.inherited(subclass)
=A0 =A0 =A0 puts "C&#= 39;s singleton class just got subclassed by #{subclass}"
=A0= =A0 end
=A0 end
=A0 def self.inherited(subclass)
=A0 =A0 p= uts "#{self} just got subclassed by #{subclass}."
=A0 e= nd
end


However, onl= y C.inherited is called, not C.singleton_class.inherited




FWIW, I th= ink it's probably always a bad idea to use hooks like this. I just can&= #39;t think of anything good you can do with it (see Rails for an example o= f how even seemingly good uses become abuse).
--089e01493c8a1f140604dca6dc61--