# So lost on singleton, metaclass and virtual class

**URL:** <https://www.ruby-forum.com/t/so-lost-on-singleton-metaclass-and-virtual-class/105785>\
**Category:** Ruby\
**Created:** [June 2, 2007, 6:36pm UTC](https://www.ruby-forum.com/t/so-lost-on-singleton-metaclass-and-virtual-class/105785 "2007-06-02T18:36:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dorren](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/848f3c/32.png) [@Dorren](https://www.ruby-forum.com/u/Dorren)\
**Post date:** [June 2, 2007, 6:36pm UTC](https://www.ruby-forum.com/t/so-lost-on-singleton-metaclass-and-virtual-class/105785/1 "2007-06-02T18:36:15Z")

</div>

Q1: in pick axe book page 364, it talks about metaclass should be a  
singleton class. but when I run the following code, class’s object\_id  
are the same for all instance, but the metaclass object\_id are  
different for each object instance, is the code wrong or what?

# === begin code

# snippet from

[http://whytheluckystiff.net/articles/seeingMetaclassesClearly.html](http://whytheluckystiff.net/articles/seeingMetaclassesClearly.html)  
class Object

# The hidden singleton lurks behind everyone

def metaclass; class \<\< self; self; end; end

def show\_meta  
puts “--------- #{to\_s} ------------”  
puts “oid: #{object\_id} #{self.class.name}”  
puts “class.oid: #{self.class.object\_id}  
#{self.class.class.name}”  
puts “metaclass.oid: #{metaclass.object\_id}  
#{self.class.metaclass.class.name}”

```
o = Object.new # show methods
meta = metaclass
puts "object methods: #{(public_methods -

```

o.public\_methods).sort.inspect}"  
puts " class methods: #{(self.class.public\_methods -  
o.class.public\_methods).sort.inspect}"  
puts " meta methods: #{(meta.public\_methods -  
Class.public\_methods).sort.inspect}"  
puts  
end  
end

# ------- fixtures for our test --------------

class A  
def initialize(v); @v = v; end  
def to\_s; @v; end

def m1; "method1 " end  
def self.class\_m0; “class method0”; end  
end

b = A.new(“b”)  
c = A.new(“c”)  
b.show\_meta  
c.show\_meta

# === end code

Q2: from pick axe book 364-367, it used singleton, metaclass, and  
virtual class definition interchangeably. are they all the same thing  
or not?

Q3: from page 365,  
“To handle per-object attributes, Ruby provides a classlike some-  
thing for each object that is sometimes called a singleton class.”  
if runtime create something new for EACH object, how can it be called  
singleton?

---

<div class="post-metadata">

**Author:** ![Dorren](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/848f3c/32.png) [@Dorren](https://www.ruby-forum.com/u/Dorren)\
**Post date:** [June 2, 2007, 6:46pm UTC](https://www.ruby-forum.com/t/so-lost-on-singleton-metaclass-and-virtual-class/105785/2 "2007-06-02T18:46:16Z")

</div>

## google group totally screwed up the code, here it is again:

# [http://whytheluckystiff.net/articles/seeingMetaclassesClearly.html](http://whytheluckystiff.net/articles/seeingMetaclassesClearly.html)

class Object

# The hidden singleton lurks behind everyone

def metaclass; class \<\< self; self; end; end

def show\_meta  
puts “--------- #{to\_s} ------------”  
puts "oid: " +  
“#{object\_id} #{self.class.name}”  
puts "class.oid: #{self.class.object\_id} " +  
“#{self.class.class.name}”  
puts "metaclass.oid: #{metaclass.object\_id} " +  
“#{self.class.metaclass.class.name}”

```
o = Object.new # show methods
meta = metaclass
puts "object methods: " +
     "#{(public_methods - o.public_methods).sort.inspect}"
puts " class methods: " +
 "#{(self.class.public_methods -

```

o.class.public\_methods).sort.inspect}"  
puts " meta methods: " +  
“#{(meta.public\_methods - Class.public\_methods).sort.inspect}”  
puts  
end  
end

# ------- fixtures for our test --------------

class A  
def initialize(v); @v = v; end  
def to\_s; @v; end

def m1; "method1 " end  
def self.class\_m0; “class method0”; end  
end

b = A.new(“b”)  
c = A.new(“c”)  
b.show\_meta  
c.show\_meta

---

<div class="post-metadata">

**Author:** ![Dorren](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/848f3c/32.png) [@Dorren](https://www.ruby-forum.com/u/Dorren)\
**Post date:** [June 2, 2007, 7:06pm UTC](https://www.ruby-forum.com/t/so-lost-on-singleton-metaclass-and-virtual-class/105785/3 "2007-06-02T19:06:17Z")

</div>

On 02.06.2007 18:30, Dorren wrote:

> Q1: in pick axe book page 364, it talks about metaclass should be a  
> singleton class. but when I run the following code, class’s object\_id  
> are the same for all instance, but the metaclass object\_id are  
> different for each object instance, is the code wrong or what?

The term “singleton class” has been discussed a lot. Other names have  
been suggested but I guess “singleton class” sticks because the book  
uses it. 🙂

Basically it just means that there is one singleton class per instance.  
This class is invisible most of the time - you basically see it only  
when you do class\<\<self. You can imagine that this class sits between  
the instance and the original class (the return value of self.class).

> Q2: from pick axe book 364-367, it used singleton, metaclass, and  
> virtual class definition interchangeably. are they all the same thing  
> or not?

Um, I would have to check with my copy which I don’t have here with me.  
But it is very possible.

> Q3: from page 365,  
> “To handle per-object attributes, Ruby provides a classlike some-  
> thing for each object that is sometimes called a singleton class.”  
> if runtime create something new for EACH object, how can it be called  
> singleton?

See my explanation above.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![Dorren](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/848f3c/32.png) [@Dorren](https://www.ruby-forum.com/u/Dorren)\
**Post date:** [June 4, 2007, 4:31am UTC](https://www.ruby-forum.com/t/so-lost-on-singleton-metaclass-and-virtual-class/105785/4 "2007-06-04T04:31:29Z")

</div>

much thanks.

Dorren

---

<div class="post-metadata">

**Author:** ![Dorren](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/848f3c/32.png) [@Dorren](https://www.ruby-forum.com/u/Dorren)\
**Post date:** [June 3, 2007, 3:49am UTC](https://www.ruby-forum.com/t/so-lost-on-singleton-metaclass-and-virtual-class/105785/5 "2007-06-03T03:49:04Z")

</div>

On 6/2/07, Dorren [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Q1: in pick axe book page 364, it talks about metaclass should be a  
> singleton class. but when I run the following code, class’s object\_id  
> are the same for all instance, but the metaclass object\_id are  
> different for each object instance, is the code wrong or what?

Here we are talking about singleton classes used to provide instance  
specific behavior.

Singleton here means that the singleton class has only one instance.  
So each instance which needs one, has its own singleton class.

> Q2: from pick axe book 364-367, it used singleton, metaclass, and  
> virtual class definition interchangeably. are they all the same thing  
> or not?

No they arent.

Singleton classes are classes which are marked internally with a flag  
bit called FL\_SINGLETON. You don’t normally see these classes unless  
you use the class \<\<self;self;end trick.

Metaclass isn’t really a Ruby term, it’s a term from Smalltalk in  
which the classes of class objects were instances of a class named  
Metaclass. Although Ruby class object do indeed have classes, they ar  
not explicit metaclasses. Instead the Ruby implementation uses  
classes marked as singletons for this purpose. In the pickaxe, Matz is  
quoted as saying that instead of metaclasses, Ruby has singleton  
classes of classes which “behave just like Smalltalk’s metaclasses.”

Why do class objects need classes? Classes provide a place to store a  
hash of method names to methods for their instances. So when a method  
of a particular object is invoked, or equivalently as us old  
Smalltalkers like to say a message is sent to an object, the search  
for the corresponding method starts with the objects class then up a  
chain of that class’ superclass etc.

Virtual classes don’t appear in the current ruby implementation, maybe  
they did when the pickaxe was written, maybe not. I think that the  
most interesting thing Matz has to say in the Pickaxe is:

“IN THE CURRENT IMPLEMENTATION (emphasis mine) singleton classes are  
specially flagged class objects between objects and their class.  
These can be called “virtual” classes if the language implementer  
chooses.”

If you look at the current (1.8.5-1.9) implementation there are two  
kinds of class-like objects which are “between objects and their  
class.” The first are classes marked with FL\_SINGLETON which I’ve been  
talking about, the second are internal objects of type T\_ICLASS which  
represent modules included by a Module/Class or extended by an object.  
So I interpret the pickaxe term ‘virtual class’ to mean either a  
singleton class or one of these T\_ICLASS module proxies.

> Q3: from page 365,  
> “To handle per-object attributes, Ruby provides a classlike some-  
> thing for each object that is sometimes called a singleton class.”  
> if runtime create something new for EACH object, how can it be called  
> singleton?

This is really the same question as Q1.

This relates to an earlier thread here from last week. I just  
converted my reply to that thread to a blog entry at:  
[http://talklikeaduck.denhaven2.com/articles/2007/06/02/chain-chain-chain](http://talklikeaduck.denhaven2.com/articles/2007/06/02/chain-chain-chain)

Which might make some of this more understandable.

HTH

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)
