# Singleton class, metaclass, eigenclass: what do they mean?

**URL:** <https://rubytalk.org/t/singleton-class-metaclass-eigenclass-what-do-they-mean/61026>\
**Category:** ruby-talk\
**Created:** [2 December 2010 05:25 UTC](https://rubytalk.org/t/singleton-class-metaclass-eigenclass-what-do-they-mean/61026 "2010-12-02T05:25:35Z")\
**Posts on this page:** 9\
**Page:** 2

<div class="post-metadata">

**Author:** ![William\_Rutiser](https://avatars.discourse-cdn.com/v4/letter/w/ba9def/32.png) [@William\_Rutiser](https://rubytalk.org/u/William_Rutiser)\
**Post date:** [6 December 2010 14:35 UTC](https://rubytalk.org/t/singleton-class-metaclass-eigenclass-what-do-they-mean/61026/21 "2010-12-06T14:35:45Z")

</div>

Rick DeNatale wrote:

[snip]

> As an aside, which is probably more apropos the title of this thread.  
> I don't personally consider singleton\_class, eigenclass, and metaclass  
> to be equivalent terms.
> 
> In particular I reserved the term metaclass to be the 'singleton'  
> class of a class object. I put singleton in quotes here because,  
> although MRI uses the singleton class mechanism to implement  
> metaclasses, metaclasses are 'less' singleton when you consider that  
> 'singleton' methods which are found in the mtab of the object's  
> singleton class, are not shared by any other object, class methods  
> which found in the mtab of the metaclass ARE shared in the case where  
> the class in question has one or more subclasses.
> 
> The etymology of the word metaclass is similar to that of metalanguage
> 
> meta- (also met- before a vowel or h)  
> combining form  
> 1 denoting a change of position or condition : metamorphosis | metathesis.  
> 2 denoting position behind, after, or beyond: : metacarpus.  
> 3 denoting something of a higher or second-order kind : metalanguage | metonym.
> 
> i.e. the third definition of meta here.
> 
> and
> 
> metalanguage |ˈmetəˌla ng (g)wij|  
> noun  
> a form of language or set of terms used for the description or  
> analysis of another language. Compare with object language (sense 1).  
> • Logic a system of propositions about propositions.
> 
> So as I define it (and as is normal in most OO languages) a metaclass  
> is a form of class used to describe a class. A singleton class used  
> to describe singleton methods of an object is NOT a metaclass but  
> rather a singleton CLASS for that object, which inherits from the  
> objects original class, and is hidden from Ruby's reflection methods  
> because that 'virtual' bit is set.
> 
> IMHO, \_why sowed a seed of confusion when he used metaclass in a way  
> which conflicts with the common definition, in the poignant guide.
> 
> > \* Note 1: If I understand correctly, for an object that is not  
> > a class, the singleton\_class is not prepared a priori, but  
> > is generated at the time when it is called.  
> > &nbsp;&nbsp;&nbsp;&nbsp;  
> > This is true, although it's really an implementation detail. I don't  
> > think you can detect this from Ruby without the aid of a C extension  
> > to let you look for a singleton class without having one created  
> > automatically as soon as you ask for it with, say class \<\<  
> > self;self;end
> 
> I suppose I should really turn this into a blog article.

Is the word "singleton" used widely or at all in this sense in connection with languages other than Ruby? I don't recall encountering this use except for Ruby.

Do I understand that the non-existence of a singleton\_class until needed is just an implementation alternative to always having a possibly empty singleton\_class?

I am looking forward to a blog article.

---

<div class="post-metadata">

**Author:** ![Rick\_DeNatale1](https://avatars.discourse-cdn.com/v4/letter/r/bbce88/32.png) [@Rick\_DeNatale1](https://rubytalk.org/u/Rick_DeNatale1)\
**Post date:** [5 December 2010 22:49 UTC](https://rubytalk.org/t/singleton-class-metaclass-eigenclass-what-do-they-mean/61026/22 "2010-12-05T22:49:40Z")

</div>

> > There's a much simpler explanation IMHO.
> 
> ...  
> Thanks for the detailed explanation.
> 
> > The 'things' pointed to by klass pointers are either:
> 
> Could this be a typo where you intended to say 'super' pointers?

I think I should have said klass or superclass pointers.

> > 1) class objects (like Array, Hash, Object etc) if they are marked by  
> > a flag that they are 'virtual' then they are not returned by methods  
> > like class, superclass, ancestors, etc.
> 
> From my findings:
> 
> \* the ancestors and the class method always hide it (as you write above)  
> \* the 'superclass' method \_does\_ return the first singleton classes  
> pointed to by 'super'
> 
> \<code\>  
> $ irb  
> ruby-1.9.2-head \> class A \< Object; end  
> =\> nil  
> ruby-1.9.2-head \> A.singleton\_class  
> =\> #\<Class:A\>  
> ruby-1.9.2-head \> A.singleton\_class.superclass  
> =\> #\<Class:Object\>  
> \</code\>

That's a diference between Ruby 1.9 and 1.8 in 1.8.7

class B \< A  
end

B.superclass # =\> A  
b\_sing = class \<\< B; self; end

b\_sing.superclass # =\> #\<Class:Class\>

> **···**
>
> On Sun, Dec 5, 2010 at 4:24 PM, Peter Vandenabeele \<peter@vandenabeele.com\> wrote:
> 
> > On Sun, Dec 5, 2010 at 4:54 PM, Rick DeNatale \<rick.denatale@gmail.com\> wrote:
> 
> --  
> Rick DeNatale
> 
> Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
> Github: [rubyredrick (Rick DeNatale) · GitHub](http://github.com/rubyredrick)  
> Twitter: @RickDeNatale  
> WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
> LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)

---

<div class="post-metadata">

**Author:** ![Josh\_Cheek](https://avatars.discourse-cdn.com/v4/letter/j/e79b87/32.png) [@Josh\_Cheek](https://rubytalk.org/u/Josh_Cheek)\
**Post date:** [16 January 2011 13:43 UTC](https://rubytalk.org/t/singleton-class-metaclass-eigenclass-what-do-they-mean/61026/23 "2011-01-16T13:43:44Z")

</div>

Going through Metaprogramming Ruby, and in this book, they went with  
eigenclasses. On page 119, Paolo Perrotta says

\<\<QUOTE  
The name eigenclass has an eventful history. Each Ruby programmer seems to  
have a pet name for these entities. Most people still call them singleton  
classes, but this name confusingly recalls the (unrelated) Singleton design  
pattern. Other people call them "metaclasses," meaning "the class of a  
class." This is still a fashionable name for eigenclasses of classes, but it  
doesn't really fit eigenclasses of objects.

Yukihiro "Matz" Matsumoto, the author of Ruby, hasn't announced an official  
name yet -- but he seems to like the mathematically-friendly name  
eigenclass. The German word eigen roughly means "one's own," so the common  
translation of eigenclass is something like "an object's own class." This  
book sticks with Matz's vocabulary, but be aware that the term eigenclass is  
not as widely used as singleton class.

I also faced another terminology problem while writing this book: what do I  
call the methods of an eigenclass? Neither eigenmethods nor eigenclass  
methods is easy on the eye. After a lot of arguing and coffee drikning, I  
decided to stick with Singleton Methods, which is still the most common name  
for these things  
QUOTE

Okay, so, a few questions, then.  
1. Is this already outdated, now that there is Object#singleton\_class ?  
After this thread, I had taken to calling them singleton classes, but then I  
got to this spot in the book, and now I'm not sure again.  
2. Why do people say that singleton class is unrelated to the singleton  
design pattern, when the singleton design pattern is a way to define a class  
with only one instance, and an objects singleton class has the object as its  
only instance?

---

<div class="post-metadata">

**Author:** ![Peter\_Vandenabeele1](https://avatars.discourse-cdn.com/v4/letter/p/59ef9b/32.png) [@Peter\_Vandenabeele1](https://rubytalk.org/u/Peter_Vandenabeele1)\
**Post date:** [5 December 2010 23:49 UTC](https://rubytalk.org/t/singleton-class-metaclass-eigenclass-what-do-they-mean/61026/24 "2010-12-05T23:49:26Z")

</div>

...

> > > The 'things' pointed to by klass pointers are either:
> > 
> > Could this be a typo where you intended to say 'super' pointers?
> 
> I think I should have said klass or superclass pointers.

If I see correctly, those are really different things. The klass  
pointers for all "regular" classes (Class objects) and for virtual  
classes (singleton classes (of an object) and metaclasses (of a  
class), to follow your terminology) are all uniformily pointing to  
the Class object, so not much to see there ... What I was  
discussing is the 'super' chain that is relevant for method lookup.

> > $ irb  
> > ruby-1.9.2-head \> class A \< Object; end  
> > =\> nil  
> > ruby-1.9.2-head \> A.singleton\_class  
> > =\> #\<Class:A\>  
> > ruby-1.9.2-head \> A.singleton\_class.superclass  
> > =\> #\<Class:Object\>  
> > \</code\>
> 
> That's a diference between Ruby 1.9 and 1.8 in 1.8.7
> 
> class B \< A  
> end
> 
> B.superclass # =\> A  
> b\_sing = class \<\< B; self; end
> 
> b\_sing.superclass # =\> #\<Class:Class\>

Indeed, I saw that too and I am confused by it ...  
(that's why I only reported the 1.9.2 case that I understand).

If I understand correctly, also in ruby 1.8.7 the 'super' pointer of  
the metaclass of a class, really points to the metaclass of  
the superclass of that class (how else could B inherit the  
class methods of A ?).

So I do not understand what this means ...  
b\_sing.superclass # =\> #\<Class:Class\>

and even more confusing to me ...  
b\_sing.superclass.superclass # =\> #\<Class:Class\>

It looks like there was a convention to report superclass of all virtual  
classes to be #\<Class:Class\> (I do not understand that answer).

Thanks for all you time into this ...

Peter

> **···**
>
> On Sun, Dec 5, 2010 at 11:49 PM, Rick DeNatale \<rick.denatale@gmail.com\> wrote:

---

<div class="post-metadata">

**Author:** ![Jonas\_Pfenniger\_zimb](https://avatars.discourse-cdn.com/v4/letter/j/c5a1d2/32.png) [@Jonas\_Pfenniger\_zimb](https://rubytalk.org/u/Jonas_Pfenniger_zimb)\
**Post date:** [20 January 2011 15:58 UTC](https://rubytalk.org/t/singleton-class-metaclass-eigenclass-what-do-they-mean/61026/25 "2011-01-20T15:58:11Z")

</div>

> 1. Is this already outdated, now that there is Object#singleton\_class ?  
> After this thread, I had taken to calling them singleton classes, but then I  
> got to this spot in the book, and now I'm not sure again.

It seems that 1.9 has more or less standardized on the singleton\_class  
and singleton\_method names, but if you look into ruby's source code,  
for example in class.c, you'll find an ENSURE\_EIGENCLASS define or  
make\_metaclass method. There was a big thread on this same  
mailing-list discussing the issue.

> **···**
>
> 2011/1/16 Josh Cheek \<josh.cheek@gmail.com\>:
> 
> 2011/1/16 Josh Cheek \<josh.cheek@gmail.com\>:
> 
> > 2. Why do people say that singleton class is unrelated to the singleton  
> > design pattern, when the singleton design pattern is a way to define a class  
> > with only one instance, and an objects singleton class has the object as its  
> > only instance?
> 
> Maybe it's because inheriting classes also inherit of the singleton  
> methods defined on the parent class, but I'm making things up. I don't  
> remember the details. Eigenclass was cool IMO, because it's a weird  
> name, and make ruby special.

---

<div class="post-metadata">

**Author:** ![Rick\_DeNatale1](https://avatars.discourse-cdn.com/v4/letter/r/bbce88/32.png) [@Rick\_DeNatale1](https://rubytalk.org/u/Rick_DeNatale1)\
**Post date:** [6 December 2010 02:00 UTC](https://rubytalk.org/t/singleton-class-metaclass-eigenclass-what-do-they-mean/61026/26 "2010-12-06T02:00:53Z")

</div>

No, the klass pointer in the object header points to the beginning of  
the chain of structures (in the C language sense) which point to  
method tables to search for method lookup, the superclass pointer is  
the 'next' link in those chains.

In the case of an object with singleton methods, klass will point to  
the singleton class and it's superclass pointer will either point to  
the objects 'real' class or to a chain of one or more module proxies  
inserted before that class if the object has extended any module.

> **···**
>
> On Sun, Dec 5, 2010 at 6:49 PM, Peter Vandenabeele \<peter@vandenabeele.com\> wrote:
> 
> > On Sun, Dec 5, 2010 at 11:49 PM, Rick DeNatale \<rick.denatale@gmail.com\> wrote:  
> > ...
> > 
> > > > > The 'things' pointed to by klass pointers are either:
> > > > 
> > > > Could this be a typo where you intended to say 'super' pointers?
> > > 
> > > I think I should have said klass or superclass pointers.
> > 
> > If I see correctly, those are really different things. The klass  
> > pointers for all "regular" classes (Class objects) and for virtual  
> > classes (singleton classes (of an object) and metaclasses (of a  
> > class), to follow your terminology) are all uniformily pointing to  
> > the Class object, so not much to see there ... What I was  
> > discussing is the 'super' chain that is relevant for method lookup.
> 
> --  
> Rick DeNatale
> 
> Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
> Github: [rubyredrick (Rick DeNatale) · GitHub](http://github.com/rubyredrick)  
> Twitter: @RickDeNatale  
> WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
> LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)

---

<div class="post-metadata">

**Author:** ![Martin\_DeMello](https://avatars.discourse-cdn.com/v4/letter/m/f17d59/32.png) [@Martin\_DeMello](https://rubytalk.org/u/Martin_DeMello)\
**Post date:** [22 January 2011 12:18 UTC](https://rubytalk.org/t/singleton-class-metaclass-eigenclass-what-do-they-mean/61026/27 "2011-01-22T12:18:31Z")

</div>

I think it's more that those of us with a maths/engineering background  
found it a familiar and comfortable term.

martin

> **···**
>
> On Thu, Jan 20, 2011 at 9:28 PM, Jonas Pfenniger (zimbatm) \<jonas@pfenniger.name\> wrote:
> 
> > Eigenclass was cool IMO, because it's a weird  
> > name, and make ruby special.

---

<div class="post-metadata">

**Author:** ![Peter\_Vandenabeele1](https://avatars.discourse-cdn.com/v4/letter/p/59ef9b/32.png) [@Peter\_Vandenabeele1](https://rubytalk.org/u/Peter_Vandenabeele1)\
**Post date:** [6 December 2010 12:32 UTC](https://rubytalk.org/t/singleton-class-metaclass-eigenclass-what-do-they-mean/61026/28 "2010-12-06T12:32:46Z")

</div>

I believe the confusion is that this is the case for "regular objects"  
and their singleton classes, but I discussed the special case of  
"class objects" and their singleton classes (aka metaclasses).

For "class objects" (e.g. class A; end), the behavior of the 'super'  
pointer is different from this standard scheme, to implement  
inheritance of class methods:  
(A.singleton\_class.superclass == A.superclass.singleton\_class  
and A.singleton\_class.superclass != A.class).

Thanks for your time discussing this,

Peter

> **···**
>
> On Mon, Dec 6, 2010 at 3:00 AM, Rick DeNatale \<rick.denatale@gmail.com\> wrote:
> 
> > On Sun, Dec 5, 2010 at 6:49 PM, Peter Vandenabeele \> \<peter@vandenabeele.com\> wrote:
> > 
> > > On Sun, Dec 5, 2010 at 11:49 PM, Rick DeNatale \<rick.denatale@gmail.com\> wrote:  
> > > ...
> > > 
> > > > > > The 'things' pointed to by klass pointers are either:
> > > > > 
> > > > > Could this be a typo where you intended to say 'super' pointers?
> > > > 
> > > > I think I should have said klass or superclass pointers.
> > > 
> > > If I see correctly, those are really different things. The klass  
> > > pointers for all "regular" classes (Class objects) and for virtual  
> > > classes (singleton classes (of an object) and metaclasses (of a  
> > > class), to follow your terminology) are all uniformily pointing to  
> > > the Class object, so not much to see there ... What I was  
> > > discussing is the 'super' chain that is relevant for method lookup.
> > 
> > No, the klass pointer in the object header points to the beginning of  
> > the chain of structures (in the C language sense) which point to  
> > method tables to search for method lookup, the superclass pointer is  
> > the 'next' link in those chains.
> > 
> > In the case of an object with singleton methods, klass will point to  
> > the singleton class and it's superclass pointer will either point to  
> > the objects 'real' class or to a chain of one or more module proxies  
> > inserted before that class if the object has extended any module.

---

<div class="post-metadata">

**Author:** ![Benoit\_Daloze](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/benoit_daloze/32/1967_2.png) [@Benoit\_Daloze](https://rubytalk.org/u/Benoit_Daloze)\
**Post date:** [27 January 2011 13:15 UTC](https://rubytalk.org/t/singleton-class-metaclass-eigenclass-what-do-they-mean/61026/29 "2011-01-27T13:15:18Z")

</div>

For me, eigenclass is just weird cool.  
But anyway, the choice has been made.

> **···**
>
> On 22 January 2011 13:18, Martin DeMello \<martindemello@gmail.com\> wrote:
> 
> > On Thu, Jan 20, 2011 at 9:28 PM, Jonas Pfenniger (zimbatm) \> \<jonas@pfenniger.name\> wrote:
> > 
> > > Eigenclass was cool IMO, because it's a weird  
> > > name, and make ruby special.
> > 
> > I think it's more that those of us with a maths/engineering background  
> > found it a familiar and comfortable term.
> > 
> > martin

[Previous page](https://rubytalk.org/t/singleton-class-metaclass-eigenclass-what-do-they-mean/61026.md?page=1)
