问题描述
我有一个位于 activeRecord 类上的 ruby 方法(停用!).但是,我似乎无法找到该方法的声明位置.
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 developers on this project, so it could be anywhere. There is a deactivate! on an unrelated class, but it doesn't seem to get called.
任何想法如何找到一个实例的所有超类,或者在哪里可以找到停用代码!?
Any ideas how to find all the superclasses for an instace, or where to find the code for deactivate!?
推荐答案
当我需要查找某个类上声明方法的位置时,请说模型",我会这样做
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! }
这会按照 ruby 搜索具有 instance_methods(false)
中方法的第一个方法的相同顺序搜索祖先树,该方法只包括非继承方法.
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.
注意:在 ruby 1.9 之前,方法被列为字符串而不是符号,所以它会
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!') }
这篇关于如何找到声明 ruby 方法的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!