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
Jonathan Jacksonreplied to me on twitter, he said, that if you are using Pry (which you should) you can actually do it even better.
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 update
Basically, the guy asks how he can run specs isolated into logical groups, running the unit without running the integration and so on.
At Gogobot, we have exactly that, since we run our specs in the CI, we can decide which to run, and they run one after the other and dependent on each other, so if you fail UNIT, the build fails, no need to continue on.