为什么Object.superclass.respond_to ?:object_method ?

时间:2022-12-30 21:31:04

I am confused. When I define a method in Object I can call it in Objects superclass BasicObject!

我困惑。当我在对象中定义一个方法时,我可以在对象超类BasicObject中调用它!

Like so:

像这样:

class Object
  def object_method
    "object_method called"
  end
end

Object.superclass.respond_to? :object_method
# => true

Object.superclass.object_method
# => "object_method called"

I have expected that only derived classes inherit the new method!

我期望只有派生类继承新方法!

P.S.: I come to this question from an exercise on rubymonk

注:我是通过一个关于rubymonk的练习来回答这个问题的

.. implement a method superclasses inside Object ..

. .在对象内部实现方法超类。

where the recursion stop criterion is "affected".

递归停止条件被“影响”。

2 个解决方案

#1


3  

When you call Object.superclass, you get an object that describes BasicObject class.

当你调用对象。超类,你得到一个描述BasicObject类的对象。

This object is an instance of Class class that inherits from Object. Therefore, it has all the methods that Object has, including the one you added.

这个对象是继承自对象的类的实例。因此,它拥有对象拥有的所有方法,包括您添加的方法。

However, instances of the BasicObject class do not have this method:

但是,BasicObject类的实例没有这种方法:

irb(main):122:0> Object.object_method
=> "object_method called"

irb(main):123:0> Object.new.object_method
=> "object_method called"

irb(main):124:0> Object.superclass.object_method  # Same as BasicObject.object_method
=> "object_method called"

irb(main):125:0> Object.superclass.new.object_method  # Same as BasicObject.new.object_method
NoMethodError: undefined method `object_method' for #<BasicObject:0x441743be>
        from (irb):125:in `evaluate'
        from org/jruby/RubyKernel.java:1065:in `eval'
        from org/jruby/RubyKernel.java:1390:in `loop'
        from org/jruby/RubyKernel.java:1173:in `catch'
        from org/jruby/RubyKernel.java:1173:in `catch'
        from ~/.rvm/rubies/jruby-1.7.0/bin/irb:13:in `(root)'

Here is more stuff to meditate on:

这里有更多的东西值得思考:

irb(main):129:0> Object
=> Object
irb(main):130:0> Object.class
=> Class
irb(main):131:0> Object.new.class
=> Object
irb(main):132:0> Object.superclass
=> BasicObject
irb(main):133:0> Object.superclass.class
=> Class
irb(main):134:0> Object.superclass.new.class
NoMethodError: undefined method `class' for #<BasicObject:0x1c944d4a>
        from (irb):134:in `evaluate'
        from org/jruby/RubyKernel.java:1065:in `eval'
        from org/jruby/RubyKernel.java:1390:in `loop'
        from org/jruby/RubyKernel.java:1173:in `catch'
        from org/jruby/RubyKernel.java:1173:in `catch'
        from ~/.rvm/rubies/jruby-1.7.0/bin/irb:13:in `(root)'

Have fun!

玩得开心!

#2


3  

As you can see, it is a derived class.

如您所见,它是一个派生类。

#ruby 1.8.7
Object.superclass
# => nil
nil.kind_of? Object
# => true

#ruby 2.0.0
Object.superclass
# => BasicObject 
BasicObject.kind_of? Object
# => true 

#1


3  

When you call Object.superclass, you get an object that describes BasicObject class.

当你调用对象。超类,你得到一个描述BasicObject类的对象。

This object is an instance of Class class that inherits from Object. Therefore, it has all the methods that Object has, including the one you added.

这个对象是继承自对象的类的实例。因此,它拥有对象拥有的所有方法,包括您添加的方法。

However, instances of the BasicObject class do not have this method:

但是,BasicObject类的实例没有这种方法:

irb(main):122:0> Object.object_method
=> "object_method called"

irb(main):123:0> Object.new.object_method
=> "object_method called"

irb(main):124:0> Object.superclass.object_method  # Same as BasicObject.object_method
=> "object_method called"

irb(main):125:0> Object.superclass.new.object_method  # Same as BasicObject.new.object_method
NoMethodError: undefined method `object_method' for #<BasicObject:0x441743be>
        from (irb):125:in `evaluate'
        from org/jruby/RubyKernel.java:1065:in `eval'
        from org/jruby/RubyKernel.java:1390:in `loop'
        from org/jruby/RubyKernel.java:1173:in `catch'
        from org/jruby/RubyKernel.java:1173:in `catch'
        from ~/.rvm/rubies/jruby-1.7.0/bin/irb:13:in `(root)'

Here is more stuff to meditate on:

这里有更多的东西值得思考:

irb(main):129:0> Object
=> Object
irb(main):130:0> Object.class
=> Class
irb(main):131:0> Object.new.class
=> Object
irb(main):132:0> Object.superclass
=> BasicObject
irb(main):133:0> Object.superclass.class
=> Class
irb(main):134:0> Object.superclass.new.class
NoMethodError: undefined method `class' for #<BasicObject:0x1c944d4a>
        from (irb):134:in `evaluate'
        from org/jruby/RubyKernel.java:1065:in `eval'
        from org/jruby/RubyKernel.java:1390:in `loop'
        from org/jruby/RubyKernel.java:1173:in `catch'
        from org/jruby/RubyKernel.java:1173:in `catch'
        from ~/.rvm/rubies/jruby-1.7.0/bin/irb:13:in `(root)'

Have fun!

玩得开心!

#2


3  

As you can see, it is a derived class.

如您所见,它是一个派生类。

#ruby 1.8.7
Object.superclass
# => nil
nil.kind_of? Object
# => true

#ruby 2.0.0
Object.superclass
# => BasicObject 
BasicObject.kind_of? Object
# => true