5

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...

  • metaclass
  • eigenclass
  • singleton class

Synonymous in Ruby? I know that metaclass means something specific and different in Python, for example, but what about in Ruby?

Max Cantor
  • 8,229
  • 7
  • 45
  • 59

3 Answers3

18

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:

  • singleton class
  • eigenclass
  • metaclass
  • ghost class
  • own class
  • virtual class
  • shadow class
  • myclass
  • selfclass
  • overclass
  • underclass
  • anchorclass
  • embedded class
  • intrinsic class
  • innate class
  • nameless class
  • unit class
  • atom class
  • singular class
  • singularity
  • bongo class
  • inner class

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.

Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653
  • 1
    So Object#singleton_class() is the official implementation of the common "def singleton_class; class << self; end; end" idiom? – Max Cantor Aug 17 '14 at 16:37
2

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

Community
  • 1
  • 1
7stud
  • 46,922
  • 14
  • 101
  • 127
  • 1
    I don't know about most-used, but it is the term used in the standard library. "Singleton class" is also the most confusing term, though, because it conflates singleton classes (i.e. eigenclasses) with singleton classes (i.e. classes that are singletons). That's why other terms are often used in discussing the concept. – Chuck Aug 16 '14 at 01:52
  • I prefer "eigenclass", but that's mostly because it's fun to say. – Max Cantor Aug 17 '14 at 16:35
  • That diagram is so needlessly confusing. Why not just say "all objects inherit from an eigenclass unique to themselves"? – Max Cantor Aug 17 '14 at 16:36
  • @MaxCantor, Because that would be incorrect. Check out the 4th diagram on the page I linked to, the one with the heading `the full lookup path for a method called on Class looks like this:` How many objects inherit from Module's singleton class? – 7stud Aug 17 '14 at 16:48
2

They all mean the same, but I think the official terms are eigenclass and singleton class. It seems like metaclass (or meta-class) sneaked in due to the document (actually comments on the source) being edited by multiple people without consensus on the terms. It looks like eigenclass was used more in the earlier days, but as English-imperialist community (many of whom do not understand the morpheme "eigen") came to take over Ruby, singleton class became popular. And Matz does not seem to want to mix non-English words into method names, so ever since the method singleton_class has been introduced, it seems like singleton class became even more dominant.

sawa
  • 165,429
  • 45
  • 277
  • 381