From: Rick DeNatale Date: 2007-12-18T00:49:49+09:00 Subject: Re: Inheritance and meta-classes On 12/16/07, Robert Klemme wrote: > On 13.12.2007 15:33, Rick DeNatale wrote: > > Here's a simplified diagram > > http://myskitch.com/rubyredrick/skitched-20071213-093049/ > > > > It's simplified since it doesn't include the inclusion of Kernel by Object. > > Nice colors. ;-) I reckon Array' is intended to mean the singleton > class of object Array. I am not sure I get your point where you wanted > to correct me. As far as I can see we are completely in sync. Yes I copied the classname' notation from the diagrams Dave Thomas uses in the pickaxe. The statement I wanted to correct, maybe I misunderstood it was: > The meta class is there but it's hidden from #ancestors as you found > out. Inheritance wise it sits between an object and its class. I read object as an instance of "its class." The above statement is wrong in two regards. First, the metaclass isn't on the inheritance chain of the instance, it's on the inheritance chain of the class, but that's a horse of a different color. Second, in the klass chain it's on the other side of the class, it goes: instance - klass-> class -klass-> class' NOT instance -klass-> class' -klass-> class > Generally singleton classes of class instances and of ordinary instances > behave the same. The only difference I can see so far is that, since > classes can inherit from each other, a subclass also shares instance > methods defined for its superclass (see my irb session). Btw, something > similar happens with instances when cloned: > > irb(main):001:0> o = Object.new > => # > irb(main):002:0> def o.xxx;1;end > => nil > irb(main):003:0> o.xxx > => 1 > irb(main):004:0> class < => ["xxx"] > irb(main):005:0> o1 = o.clone > => # > irb(main):006:0> o1.xxx > => 1 > irb(main):007:0> class < => ["xxx"] > irb(main):008:0> o2 = o.dup > => # > irb(main):009:0> o2.xxx > NoMethodError: undefined method `xxx' for # > from (irb):9 > from :0 > irb(main):010:0> class < => [] Not quite the same, what happens here is that cloning an object with a singleton class gives the clone a singleton class which looks like the original object's singleton class: k$ irb irb(main):001:0> def meta irb(main):002:1> class << self;self;end irb(main):003:1> end => nil irb(main):004:0> o = Object.new => # irb(main):005:0> o.meta => #> irb(main):006:0> o1 = o.clone => # irb(main):007:0> o1.meta => #> irb(main):008:0> o.meta == o1.meta => false One last point, you have to be careful in looking at the results from experiments like this in irb/ruby programs because some of the reflection methods in ruby deliberately lie about what's really happening under the covers. -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/