- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我真的不明白这个Ruby Monk exercise :
There's an important nuance that's worth understanding when dealing with singleton_method_added and method_added.
The most natural tack the mind takes with these two callbacks is to assume that method_added will observe instance methods and singleton_method_added, class methods. This, however, is not entirely true; Ruby's singleton object backs normal objects and contains object specific changes. For classes, these are class methods. But when adding a method to a single, regular object, adding instance methods to that object alone modifies the singleton object for that object.
Let me show you why by having you solve this exercise.
练习是:
AN_OBJECT = Object.new
def AN_OBJECT.methods_added
@@methods_added ||= []
end
def AN_OBJECT.singleton_methods_added
@@singleton_methods_added ||= []
end
我的尝试是这样做的:
def AN_OBJECT.methods_added
unless AN_OBJECT.is_a?(Class)
@@methods_added ||= []
end
end
def AN_OBJECT.singleton_methods_added
if AN_OBJECT.instance_of?(Object)
@@singleton_methods_added ||= []
end
end
但规范仍然失败。
我真的很难理解他们到底想让我做什么。什么是单例对象?据我所知,单例类是一种“元类”,它的存在只是为了给类的某个实例提供行为。
单例对象是一种“元对象”,它的存在仅仅是为了给对象的某个实例赋予行为吗?这让我很困惑。
最佳答案
Eureka !
AN_OBJECT = Object.new
def AN_OBJECT.methods_added
@@methods_added ||= []
end
def AN_OBJECT.singleton_methods_added
@@singleton_methods_added ||= []
end
def AN_OBJECT.singleton_method_added(method_name)
@@singleton_methods_added ||= []
@@singleton_methods_added << method_name
end
如果您向上滚动查看之前的练习,它应该会给您足够的提示来说明如何处理它。可惜我这么晚才发布这个..
关于ruby - Methods_added 用于 Singleton 方法,但不是 Class 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25904789/
我真的不明白这个Ruby Monk exercise : There's an important nuance that's worth understanding when dealing wit
我是一名优秀的程序员,十分优秀!