May 14 2020 • 2 min read
I stumbled across this TIL because I've been looking up advanced topics in a bid to learn new things and deepen my knowledge of Ruby & Rails.
BasicObject
is always at the end of that chain.Class
ancestor.Class
inherits from Module
which means every class is sort of a module. "Sort of" because unlike actual modules you can't mixin classes. Module
adds in instantiation, properties etc to classes.Method lookup in Rails is possible because of a few fields all Ruby objects, classes, and modules have.
objects
class
: A pointer to the class object of the object. For example, for an Fish class, calling the class
method returns Class
.iv_tbl
(Instance Variable Table): a hashtable containing the instance variables of the object.classes, modules
m_tbl
(Method Table) - A hashtable containing all the class or module's instance methods.super
- A pointer to the class or module's superclass (direct ancestor). You can lookup the ancestors of an object by calling the ancestors
method on that object.class
pointer and checks its method table (m_tbl
) for a match.super
pointer and checks its method table for a match. Ruby continues up the super
ancestor chain, looking into each ancestor's method table for a match.method_missing
on the original object. FYI, method_missing
is a method so it'll undergo this lookup process too until it is found and invoked.