A Class is not always a Class 0

Posted by Matt Williams

While playing around in irb last night, I discovered something interesting Class does not always have the same superclass:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

>> Class.superclass
=> Module
>> Class.object_id
=> -605542778
>> class Fud
>>   end
=> nil
>> Fud.class
=> Class
>> Fud.object_id
=> -606747838
>> Fud.superclass
=> Object
>> Class.class
=> Class
>> Class.class.superclass
=> Module
>> Fud.class.superclass
=> Module
>> Fud.class.object_id
=> -605542778
>> Fud.class
=> Class
>> Fud.class
=> Class
>> Fud.class.object_id
=> -605542778
>> Fud.superclass
=> Object
>> Fud.instance_of? Class
=> true
>> Class.instance_of? Class
=> true
>> Class.class.object_id
=> -605542778
>> Class.object_id
=> -605542778

Curious, huh? So, despite Fud being an instance of Class, it's superclass is Object. And Class is an instance of itself.

I believe that this has to do with the mysterious "class" object which every class has. This class object allows one to get at the guts, if you will, of a class in order to do metaprogramming.

Still it's pretty interesting...

Comments

Leave a response

Comment