Confused about the superclass

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.

Why?

robert

···

2010/2/8 Ruby Newbee <rubynewbee@gmail.com>:

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

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

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

···

On Sun, Feb 7, 2010 at 9:13 PM, Ruby Newbee <rubynewbee@gmail.com> wrote:

Hello,

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

Thanks for your helps.

----------

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 :slight_smile:
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 :slight_smile:

Ruby Newbee 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.

···

--
Posted via http://www.ruby-forum.com/\.

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

···

2010/2/8 Josh Cheek <josh.cheek@gmail.com>:

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 )

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/