《Programming Ruby 中文版第二版》P577页singleton_method_undefined方法说明有点问题
“当一个singleton方法在这个接受者中没有定义(??)时,singleton_method_undefined作为回调函数会被调用”
这是书上P577页的说法,看看官网的原文:
Invoked as a callback whenever a singleton method is undefined in the receiver
再看代码:
这是书上P577页的说法,看看官网的原文:
Invoked as a callback whenever a singleton method is undefined in the receiver
再看代码:
1
module Chatty
2
def Chatty.singleton_method_undefined(id)
3
puts "Undefining #{id.id2name}"
4
end
5
def Chatty.one() end
6
class << self
7
undef_method(:one)
8
end
9
end
10![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
查找关于 undef_method 的官网说明:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Prevents the current class from responding to calls to the named method. Contrast this with remove_method, which deletes the method from the particular class; Ruby will still search superclasses and mixed-in modules for a possible receiver.
《Programming Ruby 中文版第二版》P559页:
阻止当前类响应对这些给定方法的调用。这与remove_method形成鲜明对比;remove_method从这个特定的类中删除方法,而Ruby仍然会从它的超类和mixin的模块中寻找一个可能的接收者。
回到开头,is undefined 就不应该是“没有定义”了,而应该是“被undef_method方法调用后”。
ps:同样的错误可见p557页,关于method_undefined的说明
又ps:extended、method_undefined这两个方法在官网的文档中都没有说明,不知道是不是要发生什么变化?