# Class.class = class

**URL:** <https://www.ruby-forum.com/t/class-class-class/172248>\
**Category:** Ruby\
**Created:** [July 17, 2009, 1:54pm UTC](https://www.ruby-forum.com/t/class-class-class/172248 "2009-07-17T13:54:24Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ruby\_newbie\_72](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/838e76/32.png) [@ruby\_newbie\_72](https://www.ruby-forum.com/u/ruby_newbie_72)\
**Post date:** [July 17, 2009, 1:54pm UTC](https://www.ruby-forum.com/t/class-class-class/172248/1 "2009-07-17T13:54:24Z")

</div>

I understand that when you define a new class itÂ´s an instance of  
Class…

class Test  
end

So you can treat “Test” as an object and asks its class :

Test.class

Class

Is this Class itself an object ?

Because when i do Class.class, ruby gives me ‘Class’

So is Class an instance of itself? I donÂ´t understand the logic behind  
this ☹

Thanks

---

<div class="post-metadata">

**Author:** ![ruby\_newbie\_72](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/838e76/32.png) [@ruby\_newbie\_72](https://www.ruby-forum.com/u/ruby_newbie_72)\
**Post date:** [July 17, 2009, 2:06pm UTC](https://www.ruby-forum.com/t/class-class-class/172248/2 "2009-07-17T14:06:34Z")

</div>

Hi –

On Fri, 17 Jul 2009, Rubynewbie Rubynewbie wrote:

> Class
> 
> Is this Class itself an object ?
> 
> Because when i do Class.class, ruby gives me ‘Class’
> 
> So is Class an instance of itself? I donÂ´t understand the logic behind  
> this ☹

Yes; Class is an instance of itself. Also, Object is an instance of  
Class… but Class is a subclass of Object.

There’s a bit of circularity at the top of the class/instance diagram,  
in other words. It’s all in the interest of bootstrapping the object  
model into existence, so that everything fits in: classes (including  
Class) are instances of Class; all classes descend from Object; etc.

David

---

<div class="post-metadata">

**Author:** ![ruby\_newbie\_72](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/838e76/32.png) [@ruby\_newbie\_72](https://www.ruby-forum.com/u/ruby_newbie_72)\
**Post date:** [July 17, 2009, 2:11pm UTC](https://www.ruby-forum.com/t/class-class-class/172248/3 "2009-07-17T14:11:15Z")

</div>

Hi,

Am Freitag, 17. Jul 2009, 20:54:26 +0900 schrieb Rubynewbie Rubynewbie:

> Class
> 
> Is this Class itself an object ?

Yes, it is:

Test.class.ancestors

# =\> [Class, Module, Object, Kernel]

Class.class

# =\> Class

Class.class.ancestors

# =\> [Class, Module, Object, Kernel]

Bertram

---

<div class="post-metadata">

**Author:** ![ruby\_newbie\_72](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/838e76/32.png) [@ruby\_newbie\_72](https://www.ruby-forum.com/u/ruby_newbie_72)\
**Post date:** [July 17, 2009, 2:25pm UTC](https://www.ruby-forum.com/t/class-class-class/172248/4 "2009-07-17T14:25:17Z")

</div>

Thanks for the responses…

IÂ´m starting to see the light at the end of the tunnel…

Still…I need to do more experimentation with the language because i  
still have that “which came first, the chicken or the egg?” feeling 😉

---

<div class="post-metadata">

**Author:** ![ruby\_newbie\_72](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/838e76/32.png) [@ruby\_newbie\_72](https://www.ruby-forum.com/u/ruby_newbie_72)\
**Post date:** [July 17, 2009, 3:52pm UTC](https://www.ruby-forum.com/t/class-class-class/172248/5 "2009-07-17T15:52:20Z")

</div>

On 17 Jul 2009, at 13:25, Rubynewbie 72 wrote:

> Thanks for the responses…
> 
> I´m starting to see the light at the end of the tunnel…
> 
> Still…I need to do more experimentation with the language because i  
> still have that “which came first, the chicken or the egg?” feeling 😉

A: the omelette 🙂

Ellie

## Eleanor McHugh Games With Brains [http://slides.games-with-brains.net](http://slides.games-with-brains.net)

raise ArgumentError unless @reality.responds\_to? :reason

---

<div class="post-metadata">

**Author:** ![ruby\_newbie\_72](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/838e76/32.png) [@ruby\_newbie\_72](https://www.ruby-forum.com/u/ruby_newbie_72)\
**Post date:** [July 17, 2009, 2:14pm UTC](https://www.ruby-forum.com/t/class-class-class/172248/6 "2009-07-17T14:14:10Z")

</div>

> So is Class an instance of itself? I donÂ´t understand the logic behind  
> this ☹

Well then you’ll love this:

Class.class  
#=\> Class

Class.superclass  
#=\> Module

Class.superclass.superclass  
#=\> Object

Module.class  
#=\> Class

Object.class  
#=\> Class

Object.ancestors  
#=\> [Object, Kernel]

Kernel.class  
#=\> Module

So Object and Module are instances of a subclass of themselves, and  
Object  
inherits from Kernel, which is a Module, a subclass of Object. Fun!

The way this actually works is that a Ruby interpreter will implement  
Object  
using low-level building blocks, then implement Module and Class on top  
of  
that, then perform a bunch of voodoo to make Object and Module _look_  
like  
first-class Class instances.

Some quick rules on Ruby’s type system that I find helpful:

- Object is the base type. Everything is an Object.

- Module is a type of Object that stores Methods. A module contains a  
list  
of methods, and possibly pointers to one or more ‘parent’ modules mixed  
in  
using `include`.

- Class is a type of Module that not only stores Methods but can also  
spawn  
new Objects that have the class’s methods. Additionally, classes have  
extra  
inheritance semantics in that subclassing is more restrictive than  
mixins,  
and class methods are inherited when subclassing.

- Kernel is an _instance_ of Module that stores the core methods common  
to  
all Objects. It is mixed into Object after Module is created, thereby  
adding  
the core methods to all extant objects in the Ruby runtime.

---

<div class="post-metadata">

**Author:** ![ruby\_newbie\_72](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/838e76/32.png) [@ruby\_newbie\_72](https://www.ruby-forum.com/u/ruby_newbie_72)\
**Post date:** [July 22, 2009, 4:05am UTC](https://www.ruby-forum.com/t/class-class-class/172248/7 "2009-07-22T04:05:52Z")

</div>

On Jul 17, 2009, at 05:25, Rubynewbie 72 wrote:

> Thanks for the responses…
> 
> I´m starting to see the light at the end of the tunnel…
> 
> Still…I need to do more experimentation with the language because i  
> still have that “which came first, the chicken or the egg?” feeling 😉

Object comes first from (object.c):

```
 rb_cObject = boot_defclass("Object", 0);
 rb_cModule = boot_defclass("Module", rb_cObject);
 rb_cClass = boot_defclass("Class", rb_cModule);

```

In 1.9, it’s BasicObject that comes first.
