# About \<programming ruby, 2nc edition\>, meta class and Class

**URL:** <https://www.ruby-forum.com/t/about-programming-ruby-2nc-edition-meta-class-and-class/61185>\
**Category:** Ruby\
**Created:** [May 21, 2006, 5:03am UTC](https://www.ruby-forum.com/t/about-programming-ruby-2nc-edition-meta-class-and-class/61185 "2006-05-21T05:03:03Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![uncutstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/7993a0/32.png) [@uncutstone](https://www.ruby-forum.com/u/uncutstone)\
**Post date:** [May 21, 2006, 5:03am UTC](https://www.ruby-forum.com/t/about-programming-ruby-2nc-edition-meta-class-and-class/61185/1 "2006-05-21T05:03:03Z")

</div>

General speaking, itâ??s about the relationship between virtual  
class(metaclass ) and Class.

Itâ??s all in chapter 24.  
In Figure 24.2, Dave gives the relationship between object  
instance(a\_guitar), its class(Guitar) and its metaclass(Guitar`). . Then in section â??Class and Module Definitionsâ? , as we see in the code example, any class (for example, class Test) is a object instance of class Class. So I think Class is a metaclass of any class. Since Guitar` is a  
metaclass of Guitar, can I think Guitar` is a subclass of Class. If my  
understanding is right, I think itâ??s better Dave can add this  
relationship in Figure 24.2 and make it clearer.

Thanks in advance

uncutstone

---

<div class="post-metadata">

**Author:** ![uncutstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/7993a0/32.png) [@uncutstone](https://www.ruby-forum.com/u/uncutstone)\
**Post date:** [May 21, 2006, 9:16am UTC](https://www.ruby-forum.com/t/about-programming-ruby-2nc-edition-meta-class-and-class/61185/2 "2006-05-21T09:16:31Z")

</div>

The following is quoted from ruby-1.8.4-core-rdocs

> Classes in Ruby are first-class objectsâ??each is an instance of class Class.  
> All metaclasses are instances of the class `Classâ??.

This makes me confused. In my understanding, all metaclasses are  
subclass of the class ‘Class’ as I posted in previous post.

Somebody can explain it?

Thanks in advance.

uncutstone

---

<div class="post-metadata">

**Author:** ![uncutstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/7993a0/32.png) [@uncutstone](https://www.ruby-forum.com/u/uncutstone)\
**Post date:** [June 19, 2006, 5:01am UTC](https://www.ruby-forum.com/t/about-programming-ruby-2nc-edition-meta-class-and-class/61185/3 "2006-06-19T05:01:56Z")

</div>

uncutstone wu wrote:

> The following is quoted from ruby-1.8.4-core-rdocs
> 
> > Classes in Ruby are first-class objectsâ??each is an instance of class Class.  
> > All metaclasses are instances of the class `Classâ??.
> 
> This makes me confused. In my understanding, all metaclasses are  
> subclass of the class ‘Class’ as I posted in previous post.

My new understanding is: any metaclass(singleton class of a class  
object) is a subclass of the class ‘Class’ and also a instance of the  
class ‘Class’. Am I right?

I write a snippet of code to test this understanding:

class Object  
def metaclass  
class \<\<self  
self  
end  
end  
end

class AClass  
end

puts AClass.metaclass.class  
puts AClass.metaclass.superclass  
puts AClass.metaclass.ancestors

the result:  
Class  
#Class:Object  
Class  
Module  
Object  
Kernel  
It almost proves my understanding but there is still something I don’t  
fully understand. It is line 2 of the result, say, #Class:Ojbect.

Somebody can explain?

Thanks in advance.

uncutstone

---

<div class="post-metadata">

**Author:** ![uncutstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/7993a0/32.png) [@uncutstone](https://www.ruby-forum.com/u/uncutstone)\
**Post date:** [June 19, 2006, 5:14am UTC](https://www.ruby-forum.com/t/about-programming-ruby-2nc-edition-meta-class-and-class/61185/4 "2006-06-19T05:14:36Z")

</div>

Maybe this helps:

```
/*
 * Ruby's Class Hierarchy Chart
 *
 * +------------------+
 * | |
 * Object---->(Object) |
 * ^ ^ ^ ^ |
 * | | | | |
 * | | +-----+ +---------+ |
 * | | | | |
 * | +-----------+ | |
 * | | | | |
 * +------+ | Module--->(Module) |
 * | | ^ ^ |
 * OtherClass-->(OtherClass) | | |
 * | | |
 * Class---->(Class) |
 * ^ |
 * | |
 * +----------------+
 *
 * + All metaclasses are instances of the class `Class'.
 */

```

T.

P.S. Recently noticed Smalltalk uses the term metaclass. I wonder why  
singleton took it’s place?

---

<div class="post-metadata">

**Author:** ![uncutstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/7993a0/32.png) [@uncutstone](https://www.ruby-forum.com/u/uncutstone)\
**Post date:** [June 19, 2006, 12:31pm UTC](https://www.ruby-forum.com/t/about-programming-ruby-2nc-edition-meta-class-and-class/61185/5 "2006-06-19T12:31:37Z")

</div>

unknown wrote:

> P.S. Recently noticed Smalltalk uses the term metaclass. I wonder why  
> singleton took it’s place?

Only classes have metaclasses, but any object can have a singleton  
class. And although singleton classes of classes are similar to  
metaclasses in some ways, they are not quite the same thing as the  
Smalltalk metaclasses.

– Jim W.

---

<div class="post-metadata">

**Author:** ![uncutstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/7993a0/32.png) [@uncutstone](https://www.ruby-forum.com/u/uncutstone)\
**Post date:** [June 19, 2006, 4:50pm UTC](https://www.ruby-forum.com/t/about-programming-ruby-2nc-edition-meta-class-and-class/61185/6 "2006-06-19T16:50:46Z")

</div>

uncutstone wu wrote:

> puts AClass.metaclass.class  
> puts AClass.metaclass.superclass  
> puts AClass.metaclass.ancestors
> 
> the result:  
> Class  
> #Class:Object  
> Class  
> Module  
> Object  
> Kernel

Can somebody explain:  
Why AClass.metaclass.superclass gets “#Class:Object”?  
Why AClass.metaclass.ancestors gets “[Class,Module,Object,Kernel]”?  
Acctually, I understand this one.  
Why ancestor’s doesn’t include superclass?

Thanks in advance.

uncutstone

---

<div class="post-metadata">

**Author:** ![uncutstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/7993a0/32.png) [@uncutstone](https://www.ruby-forum.com/u/uncutstone)\
**Post date:** [June 19, 2006, 5:31pm UTC](https://www.ruby-forum.com/t/about-programming-ruby-2nc-edition-meta-class-and-class/61185/7 "2006-06-19T17:31:37Z")

</div>

> > > > > “u” == uncutstone wu [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

u\> Why AClass.metaclass.superclass gets “#Class:Object”?

What do you expect ?

u\> Why ancestor’s doesn’t include superclass?

#ancestors don’t include singleton classes

Guy Decoux

---

<div class="post-metadata">

**Author:** ![uncutstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/7993a0/32.png) [@uncutstone](https://www.ruby-forum.com/u/uncutstone)\
**Post date:** [June 19, 2006, 5:59pm UTC](https://www.ruby-forum.com/t/about-programming-ruby-2nc-edition-meta-class-and-class/61185/8 "2006-06-19T17:59:42Z")

</div>

> > > > > “u” == uncutstone wu [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

u\> Thanks very much. I think I already understand it now.

Well, if you understand it, try it with ruby 1.8.4 🙂

You use ruby 1.8.2, no ?

Guy Decoux

---

<div class="post-metadata">

**Author:** ![uncutstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/7993a0/32.png) [@uncutstone](https://www.ruby-forum.com/u/uncutstone)\
**Post date:** [June 19, 2006, 5:55pm UTC](https://www.ruby-forum.com/t/about-programming-ruby-2nc-edition-meta-class-and-class/61185/9 "2006-06-19T17:55:07Z")

</div>

ts wrote:

> > > > > > “u” == uncutstone wu [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:
> 
> u\> Why AClass.metaclass.superclass gets “#Class:Object”?
> 
> What do you expect ?
> 
> u\> Why ancestor’s doesn’t include superclass?
> 
> #ancestors don’t include singleton classes

Thanks very much. I think I already understand it now.

uncutstone

---

<div class="post-metadata">

**Author:** ![uncutstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/7993a0/32.png) [@uncutstone](https://www.ruby-forum.com/u/uncutstone)\
**Post date:** [June 20, 2006, 10:01am UTC](https://www.ruby-forum.com/t/about-programming-ruby-2nc-edition-meta-class-and-class/61185/10 "2006-06-20T10:01:08Z")

</div>

> > > > > “u” == uncutstone wu [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

u\> I think it is because 1.8.2 and 1.8.4 have different object models.

no, the object model is not changed.

the problem is here

moulon% ./ruby -ve ‘a = Object.new; p class \<\< a; self end.superclass’  
ruby 1.8.2 (2004-12-25) [i686-linux]  
Object  
moulon%

moulon% /usr/bin/ruby -ve ‘a = Object.new; p class \<\< a; self  
end.superclass’  
ruby 1.8.4 (2005-12-24) [i486-linux]  
#Class:Object  
moulon%

moulon% ./ruby -ve ‘class A \< Object; end; p class \<\< A; self  
end.superclass’  
ruby 1.8.2 (2004-12-25) [i686-linux]  
#Class:Object  
moulon%

moulon% /usr/bin/ruby -ve ‘class A \< Object; end; p class \<\< A; self  
end.superclass’  
ruby 1.8.4 (2005-12-24) [i486-linux]  
#Class:Class  
moulon%

trying to correct a bug has perhaps introduced a problem

Guy Decoux

---

<div class="post-metadata">

**Author:** ![uncutstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/7993a0/32.png) [@uncutstone](https://www.ruby-forum.com/u/uncutstone)\
**Post date:** [June 19, 2006, 7:23pm UTC](https://www.ruby-forum.com/t/about-programming-ruby-2nc-edition-meta-class-and-class/61185/11 "2006-06-19T19:23:10Z")

</div>

ts wrote:

> > > > > > “u” == uncutstone wu [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:
> 
> u\> Thanks very much. I think I already understand it now.
> 
> Well, if you understand it, try it with ruby 1.8.4 🙂
> 
> You use ruby 1.8.2, no ?
> 
> Guy Decoux

Yes, I use 1.8.2.  
And I try it in 1.8.4 and get “#Class:Class” from  
AClass.metaclass.superclass instead of “#Class:Object”.

I think it is because 1.8.2 and 1.8.4 have different object models.  
What is the defference between object models of ruby 1.8.2 and 1.8.4?  
Where can I find this kind of information?

Thanks in advance.

uncutstone

---

<div class="post-metadata">

**Author:** ![uncutstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/7993a0/32.png) [@uncutstone](https://www.ruby-forum.com/u/uncutstone)\
**Post date:** [June 20, 2006, 1:29pm UTC](https://www.ruby-forum.com/t/about-programming-ruby-2nc-edition-meta-class-and-class/61185/12 "2006-06-20T13:29:24Z")

</div>

ts wrote:

> > > > > > “u” == uncutstone wu [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:
> 
> u\> I think it is because 1.8.2 and 1.8.4 have different object models.
> 
> no, the object model is not changed.
> 
> the problem is here
> 
> moulon% ./ruby -ve ‘a = Object.new; p class \<\< a; self end.superclass’  
> ruby 1.8.2 (2004-12-25) [i686-linux]  
> Object  
> moulon%
> 
> moulon% /usr/bin/ruby -ve ‘a = Object.new; p class \<\< a; self  
> end.superclass’  
> ruby 1.8.4 (2005-12-24) [i486-linux]  
> #Class:Object  
> moulon%
> 
> moulon% ./ruby -ve ‘class A \< Object; end; p class \<\< A; self  
> end.superclass’  
> ruby 1.8.2 (2004-12-25) [i686-linux]  
> #Class:Object  
> moulon%
> 
> moulon% /usr/bin/ruby -ve ‘class A \< Object; end; p class \<\< A; self  
> end.superclass’  
> ruby 1.8.4 (2005-12-24) [i486-linux]  
> #Class:Class  
> moulon%
> 
> trying to correct a bug has perhaps introduced a problem

So, you mean this is a bug of version 1.8.4.

Best Regards,

uncutstone

---

<div class="post-metadata">

**Author:** ![uncutstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/7993a0/32.png) [@uncutstone](https://www.ruby-forum.com/u/uncutstone)\
**Post date:** [June 20, 2006, 4:04pm UTC](https://www.ruby-forum.com/t/about-programming-ruby-2nc-edition-meta-class-and-class/61185/13 "2006-06-20T16:04:47Z")

</div>

ts wrote:

> > > > > > “u” == uncutstone wu [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:
> 
> u\> So, you mean this is a bug of version 1.8.4.
> 
> Well, for me this is strange
> 
> moulon% ./ruby -ve ‘class A \< Object; end; p class \<\< A; self  
> end.superclass’  
> ruby 1.8.4 (2006-06-19) [i686-linux]  
> #Class:Class  
> moulon%
> 
> I prefer when ruby say this
> 
> moulon% ./ruby -ve ‘class A \< Object; end; p class \<\< A; self  
> end.superclass’  
> ruby 1.9.0 (2006-06-20) [i686-linux]  
> #Class:Object  
> moulon%
> 
> Guy Decoux

It’s reasonable. I agree with you.

Best regards,

uncutstone

---

<div class="post-metadata">

**Author:** ![uncutstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/7993a0/32.png) [@uncutstone](https://www.ruby-forum.com/u/uncutstone)\
**Post date:** [June 20, 2006, 1:39pm UTC](https://www.ruby-forum.com/t/about-programming-ruby-2nc-edition-meta-class-and-class/61185/14 "2006-06-20T13:39:53Z")

</div>

> > > > > “u” == uncutstone wu [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

u\> So, you mean this is a bug of version 1.8.4.

Well, for me this is strange

moulon% ./ruby -ve ‘class A \< Object; end; p class \<\< A; self  
end.superclass’  
ruby 1.8.4 (2006-06-19) [i686-linux]  
#Class:Class  
moulon%

I prefer when ruby say this

moulon% ./ruby -ve ‘class A \< Object; end; p class \<\< A; self  
end.superclass’  
ruby 1.9.0 (2006-06-20) [i686-linux]  
#Class:Object  
moulon%

Guy Decoux
