From: "Mehr, Assaph (Assaph)" Date: 2004-06-23T12:39:34+09:00 Subject: Re: A question about Class and Object > All other classes are an instance of Class (Object is an instance of Class). > What about Class? > It's an instance of itself, right? "Class" is an object, whose class is Class. :-) > To test this strange thing, I tried to subclass Class but it failed. > class MyClass < Class > end > =>TypeError: can't make subclass of Class That is because you're trying to extend the concept of Class-ness, not of a particular class. That will be too hard to maintain, so Ruby forbids it. > Now, I created an instance of Class. > aClass = Class.new > Is aClass a class like Object or just an object like obj (obj = Object.new)? > It seems like a class. You will see that: aClass.class gives Class, so aClass is an object whose class is Class. > aaClass = aClass.new #->Works! Now aaClass is an object, whose class is aClass. You will not see this in irb because you have not named aClass. Naming is done by binding it to a constant (beginning with a capital): irb(main):001:0> Fred = Class.new => Fred irb(main):002:0> aFred = Fred.new => # irb(main):003:0> Fred.class => Class irb(main):004:0> aFred.class => Fred > So Class is actually a class maker. > It's instances are classes. > That means that to make a class, I don't have to define a class. > If I just create an instance of Class, it's a class. Correct. For another example, Struct#new is also a "class maker": irb(main):005:0> struct = Struct.new "MyStructClass", :foo, :bar => Struct::MyStructClass irb(main):006:0> struct.class => Class irb(main):007:0> st1 = struct.new => # irb(main):009:0> st1.foo = 1 => 1 We have added a new class to the runtime, called MyStructClass. > When I think of Class and Object, the "chicken and egg" problem comes to my > mind. > Object is an instance of Class and Class inherits from Object. > Which one is first? (Which one should exist first?) irb(main):010:0> Object.id => 20796620 irb(main):011:0> Class.id => 20796596 So it seems that the Class class-object is created before the Object class-object :-) Try these (make sure you understand each one before going to the next): * Every object has a singleton-class associated with it. * The singleton-class is unique for that object and holds all the methods descriptions etc for that object. * All objects are created from a boiler-plate class. Until they are modified the singleton-classes for them will be identical to the class-object they were created from. * Concepts are represented as classes. * Instances of concepts are represented as objects. * "Object" is an identifier that represents the concept for "thing". Being a concept its singelton-class is Class. * "Class" is the identifier to represent the concept of "class", or the actual representation of the concept of concepts. Therefore its singelton-class is also Class. * Singleton-classes in themselves are concepts. Therefore they are represented as _objects_ of class _Class_. * Inheritance is used to signify a lot of things (specification, extension etc). In practice it means take the specification of the parent and modify it. * The minimal information you have of something is that it is a "thing". All "things" have a low common denominator. Therefore everything inherits from Object. * Class is a more specific concept than "thing". Therefore "Class" inherits from Object. * Object is a concept. Therefore it is of the class of things that are concepts. Therefore Object's class is Class. * The order of the creation of the objects is the Ruby (or any other runtime for that matter) bootstrap process. It is as immaterial a question as "what happened before the big bang": There is a magic that creates the memory allocations before you have access to the runtime. HTH. If not try books by Wittgenstein, Godel and Chomsky.