From: Robert Klemme Date: 2007-12-18T01:05:37+09:00 Subject: Re: Inheritance and meta-classes 2007/12/17, Rick DeNatale : > 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. But note that any class is also an instance. > 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. Again, since a class is also an instance I choose to use the more general term. But the statement holds true for classes as well because with regard to this there is no difference between an ordinary instance and an instance of Class (or Module for that matter). There is in fact some difference in behavior that results from this (see my posting) but the singleton class of a class sits between the instance (the class) and its class (Class). > 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 Do I understand you properly that you claim the chain is Array -> Class -> Array' If so, I strongly believe you are wrong. It's Array -> Array' -> Class Or did you mean anArray -> Array -> Array' Where Array' is the singleton class of Array like in irb(main):001:0> c=class < # This is correct. > > 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 That's why I wrote "similar" and not "same"... > singleton class gives the clone a singleton class which looks like the > original object's singleton class: > 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. Can you elaborate which methods do actually lie? Cheers robert -- use.inject do |as, often| as.you_can - without end