The docs for the Class class have an incredibly confusing diagram involving "metaclasses". I'm trying to demystify what's actually going on here. Are all three terms...
Synonymous in Ruby? I know that metaclass means something specific and different in Python, for example, but what about in Ruby?
Class method frontend that we defined earlier is nothing but an instance method defined in the metaclass for the object Developer ! A metaclass is essentially a class that Ruby creates and inserts into the inheritance hierarchy to hold class methods, thus not interfering with instances that are created from the class.
Creating Singletons using Metaclasses The singleton pattern is a design pattern that restricts the instantiation of a class to one object. It is used in cases where exactly one object is needed.
tl;dr: Yes.
In the past, nobody knew what to call them, so everybody called them something else. Here is just a small sample of the names different authors have used or proposed over time:
Originally, matz didn't want to choose a name, rather he wanted the community to settle on one. Unfortunately, it didn't. When matz and David Flanagan wrote The Ruby Programming Language, they had to choose, and they chose eigenclass (but singleton method). Eigenclass was also used in the first drafts for the ISO Ruby Language Specification (but mainly because it was very easy to run a search&replace on that name once an "official" one had been found).
However, in the later drafts and the final version of the ISO Ruby Language Specification, singleton class is used. That name was also chosen, when the Object#singleton_class
accessor method was finally formally introduced in Ruby 1.9.2 and Module#singleton_class?
was introduced in Ruby 2.1.
Metaclass was the term used by _why the lucky stiff in Why's (Poignant) Guide to Ruby (and other writings and libraries), which was highly influential on a whole generation of non-Japanese-speaking Ruby developers, and for a long time the best introduction to Ruby metaprogramming (and the only English one).
Nowadays, the terms singleton class and eigenclass seem to predominantly used (with singleton class taking over, thanks to the method names in the core library), with the occasional mention of metaclass.
Personally, I never liked metaclass, because it actually already has a different meaning: the metaclass is the class of a class. In Ruby, classes have two classes that define their behavior: their singleton class and the Class
class, either one of which could be considered their metaclass. (Although neither of those classes can do what metaclasses in other languages can do, e.g. change the rules the method lookup or inheritance.)
Virtual class is even more problematic, because not only does it have a different meaning in programming in general (a nested class that can be overridden in a subclass, as in e.g. Beta or Newspeak, and proposed but abandoned for Scala), it even already has a third different meaning within the Ruby community: virtual class is the name given inside the MRI and YARV source code to singleton classes and include classes.
Inner class also already has a meaning as synonym to nested class (a class which is a member of an outer class, just like a method).
Personally, I always liked eigenclass, in symmetry to already established technical terms such as eigenvalue, eigenvector, eigenspace, eigenfunction, eigenface, eigenwave, eigenplane, eigenstate, eigenproblem etc. However, I have now adopted the term singleton class, simply because it's easier to talk about the singleton_class
method as returning the singleton class.
Yes, the docs appear to be using metaclass to mean singleton class. Every class in the diagram inherits from a metaclass, whose name is the same as the class with some extra notation, which is also the format used for singleton classes, so they must mean singleton class.
Yes, they are all synonyms. Singleton class is the most often used term, and it makes some sense: every singleton class has only one instance.
As far as inheritance diagrams go, I've posted some that may be less confusing:
Ruby method lookup path for an object
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With