Coypond: Semantic grep for Ruby
Amit Levy 10 August 2011For the impatient: I wrote a tool to search through Ruby code. It’s available at http://github.com/alevy/coypond
I recently made some patches to the Paperclip Ruby gem for a photo hosting web application I’ve been building (called PicAcs - my gallery is here). Paperclip handles file uploads in Rails apps, and specifically makes resizing images and uploading to Amazon S3 very easy. The problem was that PicAcs allows users to use their own S3 accounts to store photos, and Paperclip doesn’t support using dynamic S3 credentials out of the box, which I definitely need. Turns out the patch isn’t too complicated (a only 3 lines of code or so), but figuring out where to patch was a pretty painful task.
This is not the fault of the Paperclip code-base, which as far as I can tell, is generally very good. The reason, I think, is a combination of Ruby’s flexibility as a language and a lack of good tools for navigating Ruby source code:
-
Ruby’s flexibility means that code for a particular module or class may be defined in multiple places (this is important for a framework like Paperclip where it needs to dynamically load code based on runtime variables)
-
As opposed to languages like Java which have heavy-weight mature IDEs like Eclipse, there’s no equivalent for Ruby where I can just
F3my way around a code base. I end up having to hold a lot more information in my short term memory with Ruby, so everything is much messier.
There was (I assume sill is) an internal tool at Google that functioned essentially like Google Code Search (which is pretty nice for searching open source C/C++/Java code, but sucks for other things like Ruby). I remember becoming so reliant on it when I was at Google, and I wished for a similar tool to file through my Ruby projects and all of their dependancies.
A couple years ago during a hackathon, David Balatero and I wrote a web based tool to do this over a particular codebase. Unfortunately, neither of us can find the code… So it may as well not have happened.
Introducing Coypond
Over the last couple days, I built Coypond - a very basic grep-like tool to tackle this task. It indexes the class, module and method names in a Ruby code base, noting the files they were found in and the locations within those files. It can search through specific files, source code directory trees, or through locally installed gems. And all this through a simple command line interface! (Yeah, I know… it’s awesome).
Check it out on GitHub: http://github.com/alevy/coypond Or you can just install it:
$ gem install coypond
And run:
$ coypond -h
for instructions.
Coypond uses ripper (a built in library as of Ruby 1.9) to generate parse trees from Ruby source files. These parse trees are then use to create an inverted index of the code, annotated with semantic information like whether the definition is a class, module or method.
To be continued…
I’d like to use this library to build something with a nicer interface (perhaps web based?). I especially want it to be very convenient to move between search and looking at code (hyperlinks tend to be good for that, no?). It would be nice to integrate somehow with rubygems.org, or GitHub in order to the user to search through gems or project source they don’t have on their local machine.
Please check out the gem if you program in Ruby and you’ve ever run into an this sort of problem. And please give me some feedback - creating issues on the GitHub project page is probably the best.

0 comments