From: Matt Mongeau Date: 2013-02-21T06:46:04+09:00 Subject: Re: THE CLASS/OBJECT CHICKEN-AND-EGG PARADOX --047d7b343b1c5cff9b04d62ee24f Content-Type: text/plain; charset=ISO-8859-1 It's not really a paradox. Take for example irb(main):001:0> a = [] => [] irb(main):002:0> a << a => [[...]] How can a contain itself? The answer is that it's a reference. This is the case for Class, the internal structure for determining the class of an object holds a reference to itself 290 struct RBasic { 291 unsigned long flags; 292 VALUE klass; 293 }; See how klass is a VALUE, in the c-land VALUE is the equivalent of an Object, so Class is an object where klass is set to point to itself. You wouldn't really be able to create a class whose superclass is itself in Ruby (at least nothing I can think of). I think this is often where people get confused, the c implementation is not under the same restrictions as the ruby implementation. Object is in a similar situation: 295 struct RObject { 296 struct RBasic basic; 297 struct st_table *iv_tbl; 298 }; You can see that Object has an RBasic struct inside of it. In class.c you can see that these cases are handled in a special way. 375 rb_cObject = boot_defclass("Object", rb_cBasicObject); 376 rb_cModule = boot_defclass("Module", rb_cObject); 377 rb_cClass = boot_defclass("Class", rb_cModule); --047d7b343b1c5cff9b04d62ee24f Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
It's not really a paradox. Take for example=
irb(main):001:0> a =3D []
=3D> []
irb(= main):002:0> a << a
=3D> [[...]]

=
How can a contain itself? The answer is that it's a reference. This is = the case for Class, the internal structure for determining the class of an = object holds a reference to itself=A0
290  struct RBasic {
 291      unsigned long flags;
 292      VALUE klass;
 293  };
See how klass is a VALUE, in the c-land VALU= E is the equivalent of an Object, so Class is an object where klass is set = to point to itself.

You wouldn't r= eally be able to create a class whose superclass is itself in Ruby (at leas= t nothing I can think of). I think this is often where people get confused,= the c implementation is not under the same restrictions as the ruby implem= entation.

Object is in a similar situation:
295  struct RObject {
 296      struct RBasic basic;
 297      struct st_table *iv_tbl;
 298  };
You can see that Object has an RBasic struct= inside of it. In class.c you can see that these cases are handled in a spe= cial way.

375 =A0 =A0 rb_cObject = =3D boot_defclass("Object", rb_cBasicObject);
376 =A0 =A0 rb_cModule =3D boot_defclass("Module", rb_cObjec= t);
377 =A0 =A0 rb_cClass =3D =A0boot_defclass("Class",= =A0rb_cModule); =A0 =A0
--047d7b343b1c5cff9b04d62ee24f--