Ruby 为什么Integer.Response_to?(:偶数?)返回false?

Ruby 为什么Integer.Response_to?(:偶数?)返回false?,ruby,Ruby,我浏览了Ruby koan,发现about_open_classes.rb koan很有趣。特别是在最后一次测试中,他们修改了Integer#even?方法。我想玩转这个概念,所以我打开了Irb并尝试运行Integer.response_to?(:偶数?),但令我惊讶的是,我得到了错误的答案。然后我尝试了Fixnum.response_to?(:偶数?,结果为false。我还尝试了Integer.respond_to?(:respond_to?并得到了真值,当我做2时,甚至?我也得到了真值。我不

我浏览了Ruby koan,发现about_open_classes.rb koan很有趣。特别是在最后一次测试中,他们修改了
Integer#even?
方法。我想玩转这个概念,所以我打开了Irb并尝试运行
Integer.response_to?(:偶数?
),但令我惊讶的是,我得到了错误的答案。然后我尝试了
Fixnum.response_to?(:偶数?
,结果为false。我还尝试了
Integer.respond_to?(:respond_to?
并得到了真值,当我做
2时,甚至?
我也得到了真值。我不知道发生了什么事。有人能告诉我遗漏了什么吗?

Fixnum的实例将
响应:即使是?
,但Fixnum类本身不会

您可以通过定义自己的测试类来了解其工作原理:

class Test
  def self.a
    "a"
  end
  def b
    "b"
  end
end

>> Test.respond_to? :a
>> true
>> Test.respond_to? :b
>> false

>> t = Test.new
>> t.respond_to? :a
>> false
>> t.respond_to? :b
>> true

既然你抢先给了我一个好答案,我就补充了我对引擎盖下发生的事情的看法。
class Test
  def self.a
    "a"
  end
  def b
    "b"
  end
end

>> Test.respond_to? :a
>> true
>> Test.respond_to? :b
>> false

>> t = Test.new
>> t.respond_to? :a
>> false
>> t.respond_to? :b
>> true