Get class location from class object

后端 未结 7 2140
北恋
北恋 2020-12-13 23:29

I have a bunch of code to look at, and now it is debugging time. Since I have never been a fan of Ruby\'s debugger I am looking for a way of going through code and reading i

7条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-14 00:06

    Bad news! I guess in run time there is no way to know what file create or defined a class in Ruby 1.8.7.

    If the project has some structure like rails, you would be able to guess it.

    But in Ruby multiple files can be defining methods for the same class Class can even be defined during run time (metaprogramming).

    That means that there might be more than one place where the class is defined. And what you look for can be spread over more than one file.

    I guess you will have to search for all definitions of Bar and see if they are inside the module Foo, or start by find all Foo definitions and check whats inside. If the code is a mess, I don't see a easy way, you will have to follow the spaguetti form point to poi. A good editor and multiple file search might help, but you will need to read through the code.

    EDIT: Some good news after all. In Ruby 1.9 there is source_location and looks like there is backport of it for 1.8.7. However, if the definition was made during runtime by a eval or so I'm not sure if it will work. I think the simplest solution is a good editor like Rubymine that usually can tell you where the code was defined.

提交回复
热议问题