Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class.superclass = Module, Module.class = Class?

Tags:

ruby

How does that compute? It's circular

Update: (in irb)

Class.superclass = Module
Module.class = Class

How is it possible to say that a Module's class is Class, when Class is Modules underclass? It's circular, chicken and egg thing.

Object: same question: Object is the root object in the object model. How can its class be Class, since Class object doesn't even exist yet?

like image 894
Vassilis Avatar asked Apr 24 '11 01:04

Vassilis


People also ask

Can a class be a module?

A class can be implemented in one or more . py files (modules). But often, we can organize a set of variables and functions into a class definition or just simply put them in a . py file and call it a module.

What is the difference between module and class in Rails?

What is the difference between a class and a module? Modules are collections of methods and constants. They cannot generate instances. Classes may generate instances (objects), and have per-instance state (instance variables).

Is a module a class Ruby?

A Module is a collection of methods, constants, and class variables. Modules are defined as a class, but with the module keyword not with class keyword.

What is superclass of String Ruby?

String object has its own class called Class . String by itself is a class as well and String's superclass is 'Object'.


2 Answers

Let's take a look at the class.c file of the MRI source code:

void Init_class_hierarchy(void)
{
    id_attached = rb_intern("__attached__");

    rb_cBasicObject = boot_defclass("BasicObject", 0);
    /* boot_defclass is defined as boot_defclass(const char *name, VALUE super) */
    rb_cObject = boot_defclass("Object", rb_cBasicObject);
    rb_cModule = boot_defclass("Module", rb_cObject);
    rb_cClass =  boot_defclass("Class",  rb_cModule);

    /* Very important line: */
    RBASIC(rb_cClass)->klass
          = RBASIC(rb_cModule)->klass
          = RBASIC(rb_cObject)->klass
          = RBASIC(rb_cBasicObject)->klass
          = rb_cClass;
 }

These definitions in ruby.h are very important, too:

#define R_CAST(st)   (struct st*)
#define RBASIC(obj)  (R_CAST(RBasic)(obj))
#define ROBJECT(obj) (R_CAST(RObject)(obj))
#define RCLASS(obj)  (R_CAST(RClass)(obj))
#define RMODULE(obj) RCLASS(obj)

Note that Object, Module and Class are derived from BasicObject. Indeed,

irb(main):001:0> BasicObject.superclass
=> nil

Those objects are defined simultaneously and all of them have RBASIC(*)->klass = rb_cClass.

like image 87
Daniel O'Hara Avatar answered Nov 07 '22 04:11

Daniel O'Hara


x.superclass and x.class have different semantics. Observe:

irb(main):003:0> 3.superclass
NoMethodError: undefined method `superclass' for 3:Fixnum
    from (irb):3
    from :0
irb(main):004:0> 3.class
=> Fixnum

3 doesn't have a superclass because... 3 isn't a class or anything like it. But 3.class means the class that 3 is an instance of.

So the thing that should correspond to Class.superclass is not Module.class, but Module itself.

like image 26
Rachel Shallit Avatar answered Nov 07 '22 04:11

Rachel Shallit