In Ruby, is there any way to determine which files were loaded and开发者_运维知识库 defined/modified a particular class?
I don't think there's an easy way to do this correctly, but as an approximation in 1.9 you could find the source_location
for all the methods in the class:
class Class
def source_files
methods.collect { |method_name|
method(method_name).source_location[0] # just the filename, not the line number
} |
instance_methods.collect { |method_name|
instance_method(method_name).source_location[0]
}
end
end
This will also give you the files defining methods that are inherited from the superclass or included modules, which I'm not sure whether you want. There are ways to modify a class besides defining methods in it, but this doesn't detect them, so it's not perfect.
精彩评论