confused about the superclass

R

Ruby Newbee

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.
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

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 :)
 
R

Robert Klemme

2010/2/8 Josh Cheek said:
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
 
B

Brian Candler

Ruby said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top