The class/object chicken-and-egg paradox

Matt M. wrote in post #1098274:

You are awesome! But I am weak in C code :(. So can the same explanation
be
made using Ruby code?

If you want more details about how these things work check out the
following:

http://rhg.rubyforge.org/chapter02.html
http://rhg.rubyforge.org/chapter04.html

void
Init_class_hierarchy(void)
{
id_attached = rb_intern(“attached”);

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);

rb_const_set(rb_cObject, rb_intern("BasicObject"), rb_cBasicObject);
RBASIC(rb_cClass)->klass

= RBASIC(rb_cModule)->klass
= RBASIC(rb_cObject)->klass
= RBASIC(rb_cBasicObject)->klass
= rb_cClass;
}
This is exactly what is happening. You can see it builds all of these
first AND then sets the klass AFTER they are created.

But to break it down further…

“every object has an internal record”

This is RBasic:
295 struct RObject {
296 struct RBasic basic;
297 struct st_table *iv_tbl;
298 };
290 struct RBasic {
291 unsigned long flags;
292 VALUE klass;
293 };

" of what class it’s an instance of,"

See how RObject has an RBasic, see how RBasic has a klass. When
rb_cObject is first created klass is a null value, later it sets that
value:

“and the internal record inside the object Class”

We’re talking about RBasic again here

“points back to Class.”

RBASIC(rb_cClass)->klass
= RBASIC(rb_cModule)->klass
= RBASIC(rb_cObject)->klass
= RBASIC(rb_cBasicObject)->klass
= rb_cClass;
That is done there.

If you want more details about how these things work check out the
following:

http://rhg.rubyforge.org/chapter02.html
http://rhg.rubyforge.org/chapter04.html

On Thu, 21 Feb 2013 20:34:46 +0100, Xavier R. [email protected]
wrote:

You are awesome! But weak in C code :(. So can the same explanation be
made using Ruby code?

No. The code is in C, so obviously the explanation is for C code.

On Thu, Feb 21, 2013 at 12:03 PM, Xavier R. [email protected]
wrote:

In this regard please help me to understand the below :

“every object has an internal record of what class its an instance of,
and the internal record inside the object Class points back to Class.”

Thanks.

It’s a formal and direct explanation of the “Everything is and
Object”. In the sense of “Turtles all the way down” not “I’m my own
grandpa!”.

Review the syntax and experiment in irb with class, superclass,
ancestors on your own custom classes as well as the built in types
such as array, integers, strings. See if you can figure out how to
identify the methods associated within the inheritance chain at each
level from the instance methods to the class methods to the
superclasses methods and so forth.

Good luck

~Stu

On Feb 21, 2013, at 11:34 , “Xavier R.” [email protected] wrote:

You are awesome! But weak in C code :(. So can the same explanation be
made using Ruby code?

It is almost exactly the same with minor syntactic differences. Remove
the rb_c’s and semicolons, switch “->” to “.” and you have pretty much
the same thing in ruby.