要查看该方法的全部祖先...
irb
在会话中定义它:
class Object
def method_ancestry(method_name)
method_ancestors = []
method = method(method_name)
while method
method_ancestors << [method.owner, method.source_location]
method = method.super_method
end
method_ancestors
end
end
例如,在 Rails 控制台中,我可以这样做:
# assuming User is an ActiveRecord class
User.new.method_ancestry(:save)
=> [[ActiveRecord::Suppressor,
["/Users/me/.gem/ruby/2.3.1/gems/activerecord-5.0.1/lib/active_record/suppressor.rb", 40]],
[ActiveRecord::Transactions,
["/Users/me/.gem/ruby/2.3.1/gems/activerecord-5.0.1/lib/active_record/transactions.rb", 317]],
[ActiveRecord::AttributeMethods::Dirty,
["/Users/me/.gem/ruby/2.3.1/gems/activerecord-5.0.1/lib/active_record/attribute_methods/dirty.rb",
21]],
[ActiveRecord::Validations,
["/Users/me/.gem/ruby/2.3.1/gems/activerecord-5.0.1/lib/active_record/validations.rb", 43]],
[ActiveRecord::Persistence,
["/Users/me/.gem/ruby/2.3.1/gems/activerecord-5.0.1/lib/active_record/persistence.rb", 124]]]
仅此列表并不能告诉您列出的任何方法定义是否实际调用super
或只是覆盖其继承定义。但是,如果您super
在其中一个中看到,它会转到列表中的下一个。
如果你经常使用它,你可以把它放在你的~/.irbrc
or~/.pryrc
中。