In Ruby, are the terms "metaclass", "eigenclass" and "singleton class" completely synonymous and interchangeable?

The class docs for Class have an incredibly convoluted diagram that includes "metaclasses". I am trying to demystify what is actually happening here. All three words ...

  • metaclass
  • eigenclass
  • singleton class

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

+3


source to share


3 answers


tl; dr: Yes.

In the past, no one knew what to call them, so everyone called them something else. Here are just a small selection of names that different authors have used or suggested over time:

  • singleton class
  • eigenclass
  • metaclass
  • ghost class
  • own class
  • virtual classroom
  • shadow class
  • MyClass
  • selfclass
  • overclass
  • poor
  • anchorclass
  • built-in class
  • inner class
  • Congenital class
  • unnamed class
  • device class
  • atom class
  • special class
  • feature
  • bongo class
  • inner class

Initially, Matz did not want to choose a name; rather, he wanted the community to settle on one. Unfortunately, this has not happened. When matz and David Flanagan were writing the Ruby programming language , they had to choose, and they chose eigenclass (but the singleton method). Eigenclass was also used in the first drafts of the Ruby ISO language specification (but mainly because it was very easy to run a search and replace that name after the "official" one was found ).

However, in later drafts and in the final version of the ISO Ruby language specification , the singleton class is used. This name was also chosen when the accessor Object#singleton_class

was finally introduced in Ruby 1.9.2 and Module#singleton_class?

was introduced in Ruby 2.1.

Metaclass was a term used by _why lucky tough in the Why (Poignant) Guide to Ruby (and other writings and libraries), which has greatly influenced an entire generation of non-Japanese Ruby developers, and has long been the best introduction to Ruby metaprogramming (and the only English one).



Currently, the terms singleton class and eigenclass seem to be used predominantly (using the singleton class, thanks to the method names in the main library), with the metaclass sometimes mentioned.

Personally, I never liked the metaclass because it actually already has a different meaning: a metaclass is a class of a class. In Ruby, classes have two classes that define their behavior: their singleton class and a class Class

, one of which can be considered their metaclass. (Although none of these classes can do what metaclasses in other languages ​​can do, such as changing method lookup or inheritance rules.)

The virtual class is even more problematic because not only does it have different meanings in programming in general (a nested class that can be overridden in a subclass like Beta or Newspeak, and is suggested, but left to Scala), it even has a third meaning in the Ruby community: virtual class is the name specified inside the MRI and YARV source code for singleton classes and includes classes.

An inner class also makes sense as a synonym for a nested class (a class that is a member of an outer class is just like a method).

Personally, I have always liked the eigenclass in symmetry to already established technical terms such as eigenvalue, eigenvector, eigenspace, eigenfunction, eigenfunction, eigenwave, eigenplane, eigenstate, own problem, etc. However, I have now adopted the term singleton class, simply because it's easier to talk about a method singleton_class

as returning a singleton class.

+13


source


Yes, the docs seem to use a metaclass to refer to a singleton class. Each chart class inherits from a metaclass, which has the same name as a class with some additional notation, which is also the format used for singleton classes, so they must mean singleton class.

Yes, they are all synonymous. The Singleton class is the most commonly used term, and it makes sense: each singleton class has only one instance.



Regarding inheritance diagrams, I've posted some that might be less confusing:

Ruby method lookup path for object

+2


source


They all mean the same thing, but I believe the official terms are eigenclass and singleton class. It seems that the metaclass (or metaclass) sneaked out due to a document (actually comments on the source) that is being edited by a few people without consensus on terms. It appears to have been used earlier in the early days, but as the Anglo-imperialist community (many of whom do not understand the morpheme "own") took part in the ruby, the singleton class became popular. And Matz doesn't seem to want to mix non-English words with method names, so since the method was introduced singleton_class

, it seems that the singleton class has become even more dominant.

+1


source







All Articles