开发者

Finding the files that make up a Ruby class

开发者 https://www.devze.com 2023-01-25 15:01 出处:网络
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 approximatio

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消