Find the source location of a method in your project
Ruby/Rails project sometimes make it hard to find the source location of a method.
Sometimes, Duck typing, meta-programming and just plain bad design make it really hard to find where a method was defined and how.
Luckily, there’s an easy way around this.
So, here’s an example.
A few moments ago, I came across this line:
First thing I did (obviously) was going to the user class and searching for this method. Obvious enough (or this post would not exist) I could not find it.
I immediately realized it’s being defined by some cache mechanism we have.
So, I opened up a console and typed this:
And it returned
So, the method IS defined on the user somewhere, you can see exactly where like so:
This will give you an exact location where the method is defined, I found this line:
This trick saves me time, hope it will save you some browsing around helplessly, especially in bigger non-single-dev projects
EDIT: 12 APR 2012 10:51PM
I posted a link to this post in the dev chat room on Campfire.
Steve, had an awesome comment:
We are using Octopus gem in our application (Gogobot), this gem is used to connect to databases with master/slave architecture.
Just as a general warning, you may get some results like that. but if you follow the source location, it’s usually a very good hint of where the method is actually defined and in any case, I think those Proxy situations are rare.
Awesome comment nonetheless, so I though I might update the post with it.
EDIT: 12 APR 2012 11:01PM
if you open up the console and do:
This will open up the default editor (Vim in my case) in the exact location where the method is defined and you are able to edit it in place.
Another awesome comment that was worth a post updateSubmit to Hacker News
[NOTE] This post has been imported to Hugo from a Wordpress blog. It may be broken, missing images, code snippets or other information.
[NOTE] updated April 12, 2012. It's 2338 days old . This article may have outdated content.