# How to understand

**URL:** <https://www.ruby-forum.com/t/how-to-understand/176199>\
**Category:** Ruby\
**Created:** [September 26, 2009, 7:48am UTC](https://www.ruby-forum.com/t/how-to-understand/176199 "2009-09-26T07:48:44Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![haoqi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ed655f/32.png) [@haoqi](https://www.ruby-forum.com/u/haoqi)\
**Post date:** [September 26, 2009, 7:48am UTC](https://www.ruby-forum.com/t/how-to-understand/176199/1 "2009-09-26T07:48:44Z")

</div>

#object.c:  
Init\_Object()  
{  
rb\_cObject = boot\_defclass(“Object”, 0);  
rb\_cModule = boot\_defclass(“Module”, rb\_cObject);  
rb\_cClass = boot\_defclass(“Class”, rb\_cModule);

}  
=======boot\_defclass========  
boot\_defclass(name, super)  
{  
VALUE obj = rb\_class\_boot(super);  
}  
========rb\_class\_boot========  
rb\_class\_boot(super)  
{  
OBJSETUP(klass, rb\_cClass, T\_CLASS);  
}  
ï¼ƒï¼ƒï¼ƒï¼ƒï¼ƒï¼ƒï¼ƒï¼ƒï¼ƒï¼ƒï¼ƒï¼ƒï¼ƒï¼ƒï¼ƒï¼ƒï¼ƒï¼ƒï¼ƒï¼ƒï¼ƒï¼ƒï¼ƒï¼ƒï¼ƒï¼ƒï¼ƒï¼ƒï¼ƒï¼ƒï¼ƒï¼ƒï¼ƒï¼ƒï¼ƒï¼ƒ  
when call “boot\_defclass(“Object”, 0)”,“rb\_cClass” is not defined,why  
“rb\_class\_boot” can call “rb\_cClass”?

---

<div class="post-metadata">

**Author:** ![haoqi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ed655f/32.png) [@haoqi](https://www.ruby-forum.com/u/haoqi)\
**Post date:** [September 28, 2009, 9:48am UTC](https://www.ruby-forum.com/t/how-to-understand/176199/2 "2009-09-28T09:48:34Z")

</div>

> when call “boot\_defclass(“Object”, 0)”,“rb\_cClass” is not defined,why  
> “rb\_class\_boot” can call “rb\_cClass”?

Dear Curious Friend

I’m not a Ruby expert, nor do I know much about C. But since your  
curiosity infected me, I ventured into the Ruby source trying to figure  
this out. The following are my findings.

rb\_cObject, rb\_cModule, rb\_cClass all get allocated in NEWOBJ as struct  
RClass.

NEWOBJ(obj, struct RClass);

In, OBJSETUP, they’re cast to type struct RObject, and the structs  
member klass gets assigned rb\_cClass, which is a VALUE pointer:

OBJSETUP(obj, klass, flags);

#define OBJSETUP(obj,c,t) do {  
/_…_/  
RBASIC(obj)-\>klass = ©;  
/_…_/  
} while (0)

Here, rb\_cClass gets assigned an RClass type like rb\_cObject, and  
rb\_cModule. But it will also have a klass member that still holds a  
pointer to the same struct, which rb\_cClass pointed to before:  
rb\_cClass = boot\_defclass(“Class”, rb\_cModule);

As pointed out in the beginning, I’m not an expert on this, so the above  
might very well be incorrect or incomplete. I would very much appreciate  
it if someone with better insight could provide a more thorough  
explanation and elaborate on why rb\_cClass is needed.

Regards  
wisccal
