Created: Thu Oct 25 06:16:19 CEST 2018
Last modiﬁed: Thu Dec 27 09:45:25 CET 2018
Borrowed from Ruby Under A Microscope; expect language abuse.
Ruby is an object oriented language.
Every value is an object.
A program is a set of objects and the messages sent between them.
Objects share a common data structure named RObject.
RObject has space for another data structure: RBasic.
RBasic holds the class pointer (
RObject consists of a pointer to RBasic, the instance variables count, and an array of instance variables.
Generic datatypes are not stored in RObject.
Generic datatypes introduce new data structures such as RString, RArray or RRegexp.
Those data structures also have an RBasic slot.
Because those data structures have a class pointer, their instances are Ruby objects.
Simple values, such as small integers or symbols are often stored directly inside of the VALUE pointer, which usually points to the current object.
Even generic objects have instance variables.
VALUE is actually a data type at implementation level.
RObject being implemented with a union, Ruby will store instance variables in a static array iff it is more efﬁcient to do so.
generic_iv_tbl is a hash. It maps generic objects to other hashes.
Each one mapping an instance variable’s name to a value.
Each time an instance variable is allocated,
ivptr (the array of
instance variables in RObject) is made bigger; anticipating for more
Because of this, allocating space for instance variables takes a variable amount of time.
Ruby classes are all instances of the Class class; therefore classes are objects.
klass in RBasic is now
const; meaning an object cannot change class.
iv_index_tbl or Instance Variables Index Table maps an instance
variable’s id to the value’s index in
ivptr. (In RObject)
iv_index_tbl is just a shortcut/cache for a value stored in RClass.
That way, there is no need to embed instance variables name in every instances of a class.
A Ruby class is made up of a group of method definitions and a table of attribute names. A Ruby class is an object therefore, a Ruby class is also made up of an RBasic slot.
A Ruby class is also made up of
super, the pointer to the
A Ruby class has its own class-level instance variables, as well as class variables.
A class variable (
@@) is in scope in every subclass.
A class-level instance variable (
@ outside of a method) is in scope in
every subclass as well; simply, subclasses might not share the same
value for it.
Both class variables and class-level instance variables ids are stored
in the same data structure. The extra
@ allows Ruby to distinguish.
When saving a class variable, Ruby will use the higher superclass that deﬁnes it.
RClass has a slot named the constants table.
Basically, RClass holds four hash tables: one for instance methods, one for instance variables, one for class variables and another one for class constants.