cassianoleal
cassianoleal

Reputation: 2566

How do I debug Ruby's 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?

Upvotes: 1

Views: 344

Answers (1)

Johannes Fahrenkrug
Johannes Fahrenkrug

Reputation: 44700

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.

Upvotes: 1

Related Questions