# Class can be subclassed after all

**URL:** <https://rubytalk.org/t/class-can-be-subclassed-after-all/4741>\
**Category:** ruby-talk\
**Created:** [14 February 2003 23:23 UTC](https://rubytalk.org/t/class-can-be-subclassed-after-all/4741 "2003-02-14T23:23:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chris18](https://avatars.discourse-cdn.com/v4/letter/c/e8c25b/32.png) [@Chris18](https://rubytalk.org/u/Chris18)\
**Post date:** [14 February 2003 23:23 UTC](https://rubytalk.org/t/class-can-be-subclassed-after-all/4741/1 "2003-02-14T23:23:46Z")

</div>

While this may be of interest to no one but myself, there is one subclass of  
Class after all! `osc’ stands for Object Singleton Class…

irb(main):001:0\> osc = class \<\< Object; self; end  
#Class:Object  
irb(main):002:0\> osc.class  
Class  
irb(main):003:0\> osc.superclass  
Class

Interestingly, it means that we can (sort of) instantiate the singleton  
classes of any class:

irb(main):004:0\> osc.new  
#Class:0x400d4404

However, the objects returned are not _really_ instances of the singleton  
classes:

irb(main):005:0\> osc.new.class  
Class

Yeah… that’s weird…

🙂

Chris

---

<div class="post-metadata">

**Author:** ![Chris18](https://avatars.discourse-cdn.com/v4/letter/c/e8c25b/32.png) [@Chris18](https://rubytalk.org/u/Chris18)\
**Post date:** [14 February 2003 23:54 UTC](https://rubytalk.org/t/class-can-be-subclassed-after-all/4741/2 "2003-02-14T23:54:38Z")

</div>

Oops… my bad. Must have copied from the wrong irb session. The following  
is still true:

irb(main):001:0\> osc = class \<\< Object; self; end  
#Class:Object  
irb(main):002:0\> osc.class  
Class  
irb(main):003:0\> osc.superclass  
Class

…but the rest was not.

Sorry,

Chris

---

<div class="post-metadata">

**Author:** ![Mauricio\_Fernndez](https://avatars.discourse-cdn.com/v4/letter/m/e495f1/32.png) [@Mauricio\_Fernndez](https://rubytalk.org/u/Mauricio_Fernndez)\
**Post date:** [15 February 2003 00:26 UTC](https://rubytalk.org/t/class-can-be-subclassed-after-all/4741/3 "2003-02-15T00:26:18Z")

</div>

> While this may be of interest to no one but myself, there is one subclass of  
> Class after all! `osc’ stands for Object Singleton Class…
> 
> irb(main):001:0\> osc = class \<\< Object; self; end  
> #Class:Object  
> irb(main):002:0\> osc.class  
> Class  
> irb(main):003:0\> osc.superclass  
> Class

> > osc = class \<\< Object; self; end  
> > =\> Class  
> > osc.id  
> > =\> 538076786  
> > osc.superclass.id  
> > =\> 538076796  
> > Class.id  
> > =\> 538076796  
> > class \<\< Class; self; end.id  
> > =\> 538076766  
> > class \<\< Class; self; end.superclass.id  
> > =\> 538076776  
> > class \<\< Class; self; end.superclass.superclass.id  
> > =\> 538076786  
> > class \<\< Module; self; end.id  
> > =\> 538076776  
> > Class.public\_methods.find {|x| x == “new” }  
> > =\> “new”  
> > Class.singleton\_methods.find {|x| x == “new” }  
> > =\> “new”

So Class is at the top of the singleton classes’ inheritance chain.  
Interesting loop

```
           Class
             ^
Object --> Object'
  ^ ^
Module --> Module'
  ^ ^
Class --> Class'

```

> Interestingly, it means that we can (sort of) instantiate the singleton  
> classes of any class:
> 
> irb(main):004:0\> osc.new  
> #Class:0x400d4404

As you wrote in your other mail this doesn’t work.  
But I don’t see the need for it (even for fun) as we can clone the object  
with its singleton and all…

> **···**
>
> On Sat, Feb 15, 2003 at 08:23:46AM +0900, Chris Pine wrote:
> 
> –  
> \_ \_
> 
> > > \_\_ \_\_ _| |_ \_\_\_ \_ \_\_ \_\_\_ \_\_ \_ \_ \_\_  
> > > '\_ \ / _`| __/__ | '_` \_ \ / ` | ’ \   
> > > ) | (| | |_\_\_ \ | | | | | (_| | | | |  
> > > .\_\_/ \_,_|\ __|__ /| |_| |_|\__,_|_| |\_|  
> > > Running Debian GNU/Linux Sid (unstable)  
> > > batsman dot geo at yahoo dot com
> 
> Are we going to make an emacs out of apt?  
> APT - Debian in a program. It even does your laundry  
> – Seen on #Debian

---

<div class="post-metadata">

**Author:** ![Chris18](https://avatars.discourse-cdn.com/v4/letter/c/e8c25b/32.png) [@Chris18](https://rubytalk.org/u/Chris18)\
**Post date:** [15 February 2003 00:41 UTC](https://rubytalk.org/t/class-can-be-subclassed-after-all/4741/4 "2003-02-15T00:41:47Z")

</div>

So Class is at the top of the singleton classes’ inheritance chain.  
Interesting loop

```
           Class
             ^
Object --> Object'
  ^ ^
Module --> Module'
  ^ ^
Class --> Class'

```

> **···**
>
> ----- Original Message -----  
> From: “Mauricio Fernández” [batsman.geo@yahoo.com](mailto:batsman.geo@yahoo.com)
> 
> * * *
> 
> I think the rule for determining the superclass of obj’s singleton class is  
> this:
> 
> If obj has a superclass (i.e. all classes but Object), then use the  
> superclass’s singleton class. (So `superclass' and`singleton\_class’  
> operations commute.)
> 
> Otherwise, just use `obj.class’.
> 
> Chris

---

<div class="post-metadata">

**Author:** ![Mauricio\_Fernndez](https://avatars.discourse-cdn.com/v4/letter/m/e495f1/32.png) [@Mauricio\_Fernndez](https://rubytalk.org/u/Mauricio_Fernndez)\
**Post date:** [15 February 2003 09:28 UTC](https://rubytalk.org/t/class-can-be-subclassed-after-all/4741/5 "2003-02-15T09:28:30Z")

</div>

Class is the only object where obj.id == obj.class.id, isn’t it?

> **···**
>
> On Sat, Feb 15, 2003 at 09:41:47AM +0900, Chris Pine wrote:
> 
> > I think the rule for determining the superclass of obj’s singleton class is  
> > this:
> > 
> > If obj has a superclass (i.e. all classes but Object), then use the  
> > superclass’s singleton class. (So `superclass' and `singleton\_class’  
> > operations commute.)
> > 
> > Otherwise, just use `obj.class’.
> 
> –  
> \_ \_
> 
> > > \_\_ \_\_ _| |_ \_\_\_ \_ \_\_ \_\_\_ \_\_ \_ \_ \_\_  
> > > '\_ \ / _`| __/__ | '_` \_ \ / ` | ’ \   
> > > ) | (| | |_\_\_ \ | | | | | (_| | | | |  
> > > .\_\_/ \_,_|\ __|__ /| |_| |_|\__,_|_| |\_|  
> > > Running Debian GNU/Linux Sid (unstable)  
> > > batsman dot geo at yahoo dot com
> 
> Turn right here. No! NO! The OTHER right!

---

<div class="post-metadata">

**Author:** ![ts1](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@ts1](https://rubytalk.org/u/ts1)\
**Post date:** [15 February 2003 09:51 UTC](https://rubytalk.org/t/class-can-be-subclassed-after-all/4741/6 "2003-02-15T09:51:56Z")

</div>

> I think the rule for determining the superclass of obj's singleton class is  
> this:

Sincerely I don't understand why you don't look in object.c : the class  
hierarchy is described.

Guy Decoux
