How do I find where a ruby method is declared?

后端 未结 3 887
情深已故
情深已故 2020-12-29 09:40

I have a ruby method (deactivate!) that is on an activeRecord class. However, I can\'t seem to find where that method is declared.

There have been numerous develope

3条回答
  •  清酒与你
    2020-12-29 10:03

    First question would be: is it an actual method? Does obj.method(:deactivate!) raise an error?

    If it doesn't, then you can use Method#source_location(in Ruby 1.9 only, and backports can't support it):

    obj.method(:deactivate!).source_location
    

    If it does raise a NoMethodError, it is handled via method_missing. This makes it hard to track. If it accepts arguments, I'd try sending the wrong type and using the backtrace of the raised exception.

    Are you using state_machine? If you have an event transition called :deactivate, the model will have the method #deactivate! created automatically.

提交回复
热议问题