Class can be subclassed after all

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…

:slight_smile:

Chris

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

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

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


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' andsingleton_class’
operations commute.)

Otherwise, just use `obj.class’.

Chris

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!

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