How do I find where a ruby method is declared?

后端 未结 3 874
情深已故
情深已故 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:13

    When I need to find where a method is declared on some class, say 'Model', I do

    Model.ancestors.find {|c| c.instance_methods(false).include? :deactivate! }
    

    This searches the ancestor tree in the same order that ruby does for the first that has the method in instance_methods(false), which only includes non-inherited methods.

    Note: before ruby 1.9, the methods were listed as strings not symbols, so it would be

    Model.ancestors.find {|c| c.instance_methods(false).include?('deactivate!') }
    

提交回复
热议问题