# Confused about the superclass

**URL:** <https://www.ruby-forum.com/t/confused-about-the-superclass/183095>\
**Category:** Ruby\
**Created:** [February 8, 2010, 4:14am UTC](https://www.ruby-forum.com/t/confused-about-the-superclass/183095 "2010-02-08T04:14:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [February 8, 2010, 4:14am UTC](https://www.ruby-forum.com/t/confused-about-the-superclass/183095/1 "2010-02-08T04:14:16Z")

</div>

Hello,

irb(main):001:0\> class Myclass  
irb(main):002:1\> def play  
irb(main):003:2\> end  
irb(main):004:1\> end

irb(main):027:0\> Myclass.class  
=\> Class  
irb(main):028:0\> Myclass.superclass  
=\> Object  
irb(main):029:0\> Class.superclass  
=\> Module

Please see the code about, why Myclass.superclass is “Object”? But I  
think it should be “Module”.

Thanks for your helps.

---

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [February 8, 2010, 8:14am UTC](https://www.ruby-forum.com/t/confused-about-the-superclass/183095/2 "2010-02-08T08:14:49Z")

</div>

On Sun, Feb 7, 2010 at 9:13 PM, Ruby N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Hello,
> 
> Please see the code about, why Myclass.superclass is “Object”? But I  
> think it should be “Module”.
> 
> Thanks for your helps.

class MyClass  
end

MyClass.class # =\> Class  
MyClass.superclass # =\> Object  
MyClass.ancestors # =\> [MyClass, Object, Kernel]

Class.class # =\> Class  
Class.superclass # =\> Module  
Class.ancestors # =\> [Class, Module, Object, Kernel]

From here, I see that MyClass’s class and superclass are congruent with  
it’s  
ancestors, and so are Class’s. However, I am also a bit confused, it  
seems  
that if MyClass inherits from Class, then it should include Module in  
it’s  
ancestors. I thought about it a bit, and decided to check if included  
modules of Class are visible to MyClass

* * *

module DoYouSeeMe  
end

class Class  
include DoYouSeeMe  
end

MyClass.class # =\> Class  
MyClass.superclass # =\> Object  
MyClass.ancestors # =\> [MyClass, Object, Kernel]

Class.class # =\> Class  
Class.superclass # =\> Module  
Class.ancestors # =\> [Class, DoYouSeeMe, Module, Object, Kernel]

So apparently not. I decided to generalize this into a hypothesis that  
included modules are not visible to subclasses.

* * *

class MyClass  
include DoYouSeeMe  
end  
class MyInheritedClass \< MyClass  
end

MyClass.class # =\> Class  
MyClass.superclass # =\> Object  
MyClass.ancestors # =\> [MyClass, DoYouSeeMe, Object,  
Kernel]

MyInheritedClass.class # =\> Class  
MyInheritedClass.superclass # =\> MyClass  
MyInheritedClass.ancestors # =\> [MyInheritedClass, MyClass,  
DoYouSeeMe,  
Object, Kernel]

* * *

class MyClass  
include DoYouSeeMe  
end  
class MySubClass \< MyClass  
end

MyClass.class # =\> Class  
MyClass.superclass # =\> Object  
MyClass.ancestors # =\> [MyClass, DoYouSeeMe, Object,  
Kernel]

MySubClass.class # =\> Class  
MySubClass.superclass # =\> MyClass  
MySubClass.ancestors # =\> [MySubClass, MyClass, DoYouSeeMe,  
Object, Kernel]

Apparently I was wrong. I thought about it a little bit more, and  
decided  
that maybe MyClass didn’t inherit from Class, but was rather an instance  
of  
class

* * *

MyClass.instance\_of? Class # =\> true  
MySubClass.instance\_of? MyClass # =\> false  
MyOtherClass = Class.new  
MyOtherClass.ancestors # =\> [MyOtherClass, Object, Kernel]

This seems to be congruent 🙂  
So, my conclusion is that class and superclass behave correctly, in that  
they reflect their ancestry. And the reason MyClass’ ancestry is not a  
superset of Class’ ancestry is because MyClass is not a subclass of  
Class,  
but rather an instance of it. ( I tried making a subclass of Class also,  
but  
got a TypeError )

Hope that helps, thanks for asking, it was a useful exercise 🙂

---

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [February 8, 2010, 7:50am UTC](https://www.ruby-forum.com/t/confused-about-the-superclass/183095/3 "2010-02-08T07:50:49Z")

</div>

2010/2/8 Ruby N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> =\> Object  
> irb(main):029:0\> Class.superclass  
> =\> Module
> 
> Please see the code about, why Myclass.superclass is “Object”? But I  
> think it should be “Module”.

Why?

robert

---

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [February 8, 2010, 9:11am UTC](https://www.ruby-forum.com/t/confused-about-the-superclass/183095/4 "2010-02-08T09:11:35Z")

</div>

2010/2/8 Josh C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> So, my conclusion is that class and superclass behave correctly, in that  
> they reflect their ancestry. And the reason MyClass’ ancestry is not a  
> superset of Class’ ancestry is because MyClass is not a subclass of Class,  
> but rather an instance of it. ( I tried making a subclass of Class also, but  
> got a TypeError )

Exactly! You have the most important point at the end of your  
posting: with Ruby it’s so easy to confuse /inheritance/ relationship  
and /instance of/ relationship yet they are two pairs of shoes  
(although related at some point).

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [February 8, 2010, 11:06am UTC](https://www.ruby-forum.com/t/confused-about-the-superclass/183095/5 "2010-02-08T11:06:04Z")

</div>

Ruby N. wrote:

> Hello,
> 
> irb(main):001:0\> class Myclass  
> irb(main):002:1\> def play  
> irb(main):003:2\> end  
> irb(main):004:1\> end
> 
> irb(main):027:0\> Myclass.class  
> =\> Class  
> irb(main):028:0\> Myclass.superclass  
> =\> Object  
> irb(main):029:0\> Class.superclass  
> =\> Module
> 
> Please see the code about, why Myclass.superclass is “Object”? But I  
> think it should be “Module”.

All classes inherit from Object unless you give a different superclass.  
That is, when you are creating a new class(\*),

```
class Foo; end

```

is the same as

```
class Foo < Object; end

```

or

```
Foo = Class.new

```

or

```
Foo = Class.new(Object)

```

HTH,

Brian.

(\*) as opposed to re-opening an existing class, which must already have  
had its superclass chosen, and it cannot be changed.
