Ruby notes (Object sys­tem)

Created: Thu Oct 25 06:16:19 CEST 2018

Last mod­i­fied: Thu Dec 27 09:45:25 CET 2018

Borrowed from Ruby Under A Microscope; ex­pect lan­guage abuse.

Ruby is an ob­ject ori­ented lan­guage.

Every value is an ob­ject.

A pro­gram is a set of ob­jects and the mes­sages sent be­tween them.

Objects share a com­mon data struc­ture named RObject.

RObject has space for an­other data struc­ture: RBasic.

RBasic holds the class pointer (klass).

RObject con­sists of a pointer to RBasic, the in­stance vari­ables count, and an ar­ray of in­stance vari­ables.

Generic datatypes are not stored in RObject.

Generic datatypes in­tro­duce new data struc­tures such as RString, RArray or RRegexp.

Those data struc­tures also have an RBasic slot.

Because those data struc­tures have a class pointer, their in­stances are Ruby ob­jects.

Simple val­ues, such as small in­te­gers or sym­bols are of­ten stored directly in­side of the VALUE pointer, which usu­ally points to the current ob­ject.

Even generic ob­jects have in­stance vari­ables.

VALUE is ac­tu­ally a data type at im­ple­men­ta­tion level.

RObject be­ing im­ple­mented with a union, Ruby will store in­stance variables in a sta­tic ar­ray iff it is more ef­fi­cient to do so.

generic_iv_tbl is a hash. It maps generic ob­jects to other hashes. Each one map­ping an in­stance vari­able’s name to a value.

Each time an in­stance vari­able is al­lo­cated, ivptr (the ar­ray of instance vari­ables in RObject) is made big­ger; an­tic­i­pat­ing for more instance vari­ables.

Because of this, al­lo­cat­ing space for in­stance vari­ables takes a variable amount of time.

Ruby classes are all in­stances of the Class class; there­fore classes are objects.

Judging by include/ruby/ruby.h con­tents, klass in RBasic is now const; mean­ing an ob­ject can­not change class.

iv_index_tbl or Instance Variables Index Table maps an in­stance variable’s id to the val­ue’s in­dex in ivptr. (In RObject)

iv_index_tbl is just a short­cut/​cache for a value stored in RClass.

That way, there is no need to em­bed in­stance vari­ables name in every instances of a class.

A Ruby class is made up of a group of method de­f­i­n­i­tions and a table of attribute names. A Ruby class is an ob­ject there­fore, a Ruby class is also made up of an RBasic slot.

A Ruby class is also made up of super, the pointer to the su­per­class.

A Ruby class has its own class-level in­stance vari­ables, as well as class vari­ables.

A class vari­able (@@) is in scope in every sub­class.

A class-level in­stance vari­able (@ out­side of a method) is in scope in every sub­class as well; sim­ply, sub­classes might not share the same value for it.

Both class vari­ables and class-level in­stance vari­ables ids are stored in the same data struc­ture. The ex­tra @ al­lows Ruby to dis­tin­guish.

When sav­ing a class vari­able, Ruby will use the higher su­per­class that defines it.

RClass has a slot named the con­stants table.

Basically, RClass holds four hash ta­bles: one for in­stance meth­ods, one for in­stance vari­ables, one for class vari­ables and an­other one for class con­stants.

source code