查找给定类的文件路径 [英] Find file path of a given class
本文介绍了查找给定类的文件路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个给定类的 Class 对象的引用.我需要找出类的文件路径.如果有帮助,该类从 ActiveRecord 继承.
I have a reference to the Class object of a given class. I need to find out the file path of the class. The class inherits from ActiveRecord if that is any help.
有什么简单的方法吗?
谢谢.
推荐答案
在其方法上使用 source_location
:
YourClass.instance_methods(false).map { |m|
YourClass.instance_method(m).source_location.first
}.uniq
您可能会获得多个位置,因为方法可能定义在不同的位置.
You might get more than one location, as methods might be defined in different places.
这篇关于查找给定类的文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文