What comes first in the Ruby object model?

I read Metaprogramming Ruby, and the object model looked like a chicken or egg dilemma.

In Ruby 1.8, the Object class is an instance of the class. A superclass of a module is an object and is an instance of the class. The superclass class is a module, and it is an instance of the class (self-referential). Tell the class SomeClass; the end is defined somewhere; SomeClass is an instance of the class, but its superclass is Object. Why does an instance of a class have an object as a superclass instead of nil?

In addition, if Object must exist, then Class must exist, but then the module must exist, but for the module to exist, the Object must exist. How are these classes created?

+5
source share
3 answers

Ruby ( Ruby 1.8): http://banisterfiend.wordpress.com/2008/11/25/a-complete-ruby-class-diagram/

, , , , C API. ( C API) , , .

, , Init_Object() object.c ( Ruby 1.9)

rb_cBasicObject = boot_defclass("BasicObject", 0);
rb_cObject = boot_defclass("Object", rb_cBasicObject);
rb_cModule = boot_defclass("Module", rb_cObject);
rb_cClass =  boot_defclass("Class",  rb_cModule);

metaclass = rb_make_metaclass(rb_cBasicObject, rb_cClass);
metaclass = rb_make_metaclass(rb_cObject, metaclass);
metaclass = rb_make_metaclass(rb_cModule, metaclass);
metaclass = rb_make_metaclass(rb_cClass, metaclass);
boot_defmetametaclass(rb_cModule, metaclass);
boot_defmetametaclass(rb_cObject, metaclass);
boot_defmetametaclass(rb_cBasicObject, metaclass);

rb_cBasicObject BasicObject Ruby, rb_cObject - Object Ruby ..

+5

, - . . , Class , Object. ..

Object . ()

Object - BasicObject ( 1.9), BasicObject nil.

+1

You can find more information in the Ruby Hacking Guide: http://rhg.rubyforge.org/

Particularly Chapter 04: Bootstrap

+1
source

All Articles