3 ms·
A lot of people prefer the term eigenclass, but yes, it's poorly named. You're absolutely right, a Ruby metaclass is nothing more than a hidden class that acts
by vidarh 3y ago
A lot of people prefer the term eigenclass, but yes, it's poorly named.
You're absolutely right, a Ruby metaclass is nothing more than a hidden class that acts as if it exists on each object in the inheritance chain before the class you specify that you want to inherit from.
In practice it is dynamically inserted first if you define a method on it, and so the vast majority of objects do not have a meta class.
In terms of being a pointless complication, consider that if you add a field to the object, then it complicates the method call logic, because now at every call site, in addition to considering the inheritance chain, where each element is identical, you now first need to rule out whether a given method name has been overridden by a given object. (this applies to MRI; there's no inherent reason why a Ruby implementation needs to follow a chain like this - e.g. my half-finished, buggy work-in progress Ruby compiler uses C++-like tables, and instead propagates updates downwards, and so will only ever do a single indirection function pointer call, at the cost of larger class objects)
The metaclass solves that by just inserting a pointer in the inheritance chain if you're defining a method on a meta class and the object does not currently inherit from one. Then all other logic is the same other than the `class` method, which skips over classes that are meta classes.