如何调试 Ruby 的 method_missing?
让我澄清一下...
我有一个处理 XML 文件的类。此类实现 method_missing 并使用作为标记标识符传递的方法,然后返回表示该标记的 XML 节点的对象。代码或多或少是这样的:
def method_missing(m, *args, &block)
XmlNode.new(@xml.at_css(m.to_s.upcase))
end
不过,我做了一些奇怪的事情,有时 m
参数没有被发送到方法调用。
有没有办法让我查明哪个电话是罪魁祸首?
我能想到的一种方法是记录方法调用本身,但如何从 method_missing 中做到这一点?
Let me clarify this a bit...
I have a class that handles XML files. This class implements method_missing and used the method passed as the identifier of a tag, and then returns an object that represents the XML node for that tag. The code is more or less like this:
def method_missing(m, *args, &block)
XmlNode.new(@xml.at_css(m.to_s.upcase))
end
I've done something weird, though, and sometimes the m
parameter is not being sent to the method call.
Is there a way for me to pinpoint which of the calls is the culprit?
One way that I can think of is to log the method call itself, but how do I do that from within method_missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种非常粗略的方法是通过放在
XmlNode... 行的正上方来输出调用堆栈。这将输出调用堆栈,您可以看到谁调用了该方法。
A very crude way would be to output the call stack by putting
right above your XmlNode... line. That will output the call stack and you can see who called the method.