In Ruby, classes exist as objects during the execution. Of course. So there must be a structure for class objects. That structure is struct RClass. Its structure type flag is T_CLASS.
As class and modules are very similar, there is no need to differentiate their content. That’s why modules also use the struct RClass structure, and are differentiated by the T_MODULE structure flag.
▼ struct RClass
(ruby.h)
First, let’s focus on the m_tbl (Method TaBLe) member. struct st_table is an hashtable used everywhere in ruby. Its details will be explained in the next chapter “Names and name tables”, but basically, it is a table mapping names to objects. [color=darkblue]In the case of m_tbl, it keeps the correspondence between the name (ID) of the methods possessed by this class and the methods entity itself.[/color]
The fourth member super keeps, like its name suggests, the superclass. As it’s a VALUE, it’s (a pointer to) the class object of the superclass. In Ruby there is only one class that has no superclass (the root class): Object.
However I already said that all Object methods are defined in the Kernel module, Object just includes it. As modules are functionally similar to multiple inheritance, it may seem having just super is problematic, but but in ruby some clever changes are made to make it look like single inheritance. The details of this process will be explained in the fourth chapter “Classes and modules”.
Because of this, super of the structure of Object points to struct RClass of the Kernel object. Only the super of Kernel is NULL. So contrary to what I said, if super is NULL, this RClass is the Kernel object (figure 6).
As class and modules are very similar, there is no need to differentiate their content. That’s why modules also use the struct RClass structure, and are differentiated by the T_MODULE structure flag.
▼ struct RClass
300 struct RClass {
301 struct RBasic basic;
302 struct st_table *iv_tbl;
303 struct st_table *m_tbl;
304 VALUE super;
305 };
(ruby.h)
First, let’s focus on the m_tbl (Method TaBLe) member. struct st_table is an hashtable used everywhere in ruby. Its details will be explained in the next chapter “Names and name tables”, but basically, it is a table mapping names to objects. [color=darkblue]In the case of m_tbl, it keeps the correspondence between the name (ID) of the methods possessed by this class and the methods entity itself.[/color]
The fourth member super keeps, like its name suggests, the superclass. As it’s a VALUE, it’s (a pointer to) the class object of the superclass. In Ruby there is only one class that has no superclass (the root class): Object.
However I already said that all Object methods are defined in the Kernel module, Object just includes it. As modules are functionally similar to multiple inheritance, it may seem having just super is problematic, but but in ruby some clever changes are made to make it look like single inheritance. The details of this process will be explained in the fourth chapter “Classes and modules”.
Because of this, super of the structure of Object points to struct RClass of the Kernel object. Only the super of Kernel is NULL. So contrary to what I said, if super is NULL, this RClass is the Kernel object (figure 6).