如何调试 Ruby 的 method_missing?

发布于 2024-12-28 04:47:09 字数 359 浏览 4 评论 0原文

让我澄清一下...

我有一个处理 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紫罗兰の梦幻 2025-01-04 04:47:09

一种非常粗略的方法是通过放在

puts caller

XmlNode... 行的正上方来输出调用堆栈。这将输出调用堆栈,您可以看到谁调用了该方法。

A very crude way would be to output the call stack by putting

puts caller

right above your XmlNode... line. That will output the call stack and you can see who called the method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文