meta-class subclass relationships



Ruby exposes its singleton meta-classes, eg:

class <<B ; $Meta_B = self ; end

But their relationships are not quite what I expected. For example:

Suppose that A inherits from B (and B inherits from Object). Of course,
this means that A instances respond to all the B instance messages. So:

A.new.is_a? B -> true

At the same time, the A class object responds to all the B class object
messages. So:

class <<Object ; $Meta_Object = self ; end
class <<B ; $Meta_B = self ; end
class <<A ; $Meta_A = self ; end

A.is_a? $Meta_A -> true
B.is_a? $Meta_A -> false
A.is_a? $Meta_B -> true
B.is_a? $Meta_B -> true

Since everything that is_a $Meta_A also is_a $Meta_B, I expected a
subclass/superclass relationship. Indeed, I expected the following to
be true:

$Meta_A.superclass == $Meta_B
$Meta_B.superclass == $Meta_Object
$Meta_Object.superclass == Class
Class.superclass == Module
Module.superclass == Object
Object.superclass == nil

The above superclass chain reflects how a message to the class A object
is looked up. What surprised me is that the first two equalities above
are false.

Should they have been true?

FWIW: The superclass of both $Meta_A and $Meta_B is something called
#<Class:Class>, which is its own superclass and is a subclass of
$Meta_Object:

$X = $Meta_A.superclass -> #<Class:Class>
$X == $Meta_B.superclass -> true
$X == $X.superclass -> true
$X < $Meta_Object -> true
--
Posted via http://www.ruby-forum.com/.

.



Relevant Pages

  • Re: Java Typecasting Internals
    ... Percolate your way up the chain of superclasses. ... You could prepare a list of all those left superclass class object ... So checking if an object is an instance of a class is as easy as loading a pointer to the class data from the instance header, and then testing whether a pointer to the target class is at a fixed offset. ...
    (comp.lang.java.programmer)
  • Inheritance : access to the superClass methods via prototype
    ... What I am trying to achieve is an 'inherits' method similar to Douglas ... that can enable access to the superclass' priviledged methods also. ... In the following example, I create an ObjectA, an ObjectB ... the prototype is the same for all ...
    (comp.lang.javascript)
  • Re: Is there anything in C++ akin to Javas class Object?
    ... > would get linker errors when compiling files which ... > use the Object code compiled with no superclass but ... One could always implement a class Object and have any class that doesn't ... inherit anything inherit from Object explicitly. ...
    (comp.lang.cpp)
  • inheriting from dataset
    ... Hey all, ... If I create a class that inherits from dataset how do I populate the dataset ... if the class object becomes the dataset? ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Interesting take on Paradigms (OO vs Procedural)
    ... invocations to the correct code. ... If 'class object' inherits ...
    (comp.lang.cobol)