Ruby class hierarchy confusion

I'm somewhat confused about the Ruby object hierarchy.

Every class that I define is an instance of the class Class, and Class
has the hierarchy Class < Module < Object < BasicObject.

If I define a class A, then A has the hierarchy A < Object <
BasicObject.

But if A is an instance of the class Class, should the superclass of A
not be Module, like in the first hierarchy above? A's class is Class, so
it would make sense to me that it should follow the top one!!

···

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

No. A is a class and as such has a hierarchy. But since everything
in Ruby is an object, even classes are. An object is always an
instance of a class. In this case A is an instance of Class. And
Class has its own hierarchy. So you have

A < Object < BasicObject
L -instanceOf- Class < Module < Object < BasicObject

Kind regards

robert

···

On Fri, Dec 14, 2012 at 4:54 PM, Joz Private <lists@ruby-forum.com> wrote:

I'm somewhat confused about the Ruby object hierarchy.

Every class that I define is an instance of the class Class, and Class
has the hierarchy Class < Module < Object < BasicObject.

If I define a class A, then A has the hierarchy A < Object <
BasicObject.

But if A is an instance of the class Class, should the superclass of A
not be Module, like in the first hierarchy above? A's class is Class, so
it would make sense to me that it should follow the top one!!

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Hi,

I think your problem is that you confuse "instance of" and "subclass
of".

Yes, A is an *instance* of Class. But it's not a *subclass* of Class. So
there's no direct relation to Module.

···

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

If you have these classes defined:

class Parent
end

class A < Parent
end

Here is what the basic hierarchy looks like:

             +--------+ +--------+
             > Basic | | Basic |
             > Object | | Object |
             > > > >
             +--------+ +--------+
                 ^ ^
                 > >
                 > >
             +--------+ +--------+
             > > > >
             > Object | | Object |
             > > > >
             +--------+ +--------+
                 ^ ^
                 > >
                 > >
             +--------+ +--------+
             > > > >
             > Parent | | Module |
             > > > >
             +--------+ +--------+
                 ^ ^
                 > >
                 > >
              +-----+ +-------+
a = -----> | A | -----> | Class |
A.new | | | |
              +-----+ +-------+

Think about going from an object to the right into its class, and then
up the hierarchy,

···

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

7stud -- wrote in post #1089134:

Think about going from an object to the right into its class, and then
up the hierarchy to the superclass.

How about this instead: an object's class is to the right of it in the
diagram, and an object's superclass is above it. The object 'a' is not
a class, so it has no superclass and nothing appears above it in the
diagram. The object A's class is to it's right, i.e. Class, and A's
superclass is above it, i.e. Parent. Now what about Class? It's a
class because this works:

MyClass = Class.new

and because Class is a class, it is has to be an instance of Class. So
Class's *class* is Class, and as shown in the diagram Class's superclass
is Module because Class inherits from Module.

···

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