A Moment of Drug Abuse Zen

I’ve heard all sorts of opinions for and against Ruby, from “what God himself would’ve used for all its OO scripting needs” to “what Smalltalk scraped off its womb with its bare hands after being raped by Perl”, all quite amusing at the very least. A few months ago I read some introductory stuff at the website, played with it a bit, segfaulted the VM a few times, but didn’t end up doing anything particularly interesting or useful (unless you consider adding numbers and printing silly messages interesting or useful, but I digress).

Last week, though, my co-worker Obie had this very cool idea for a web app, and after considering a few technological dead ends (like, uh, Java), we decided to do it using Rails. To make it a more interesting experience, we’d pair on it while listening to things like Chicks On Speed and Crystal Method.

Indeed, and paraphrasing my colleague Wilkes Joiner, if you aren’t familiar with Rails, you’re not trying hard enough. :)

The whole thing is so tightly integrated and the work flows so nicely that I hardly feel like wanting to come back to Java and the whole “3 hours to build, test and deploy” land.

Putting the web side of things apart for a moment, as we haven’t played too much with it yet, I’d like to focus on the persistence part of Rails, ActiveRecord. From the website:

Active Record connects business objects and database tables to create a persistable domain model where logic and data is presented in one wrapping. ItÂ’s an implementation of the object-relational mapping (ORM) pattern by the same name as described by Martin Fowler (…)

Think Hibernate, but with no mapping files and no need to cut against the grain - thus removing the need for code generation tools like XDoclet - and add nifties like support for pluralization, so that the Person class gets mapped to the people table, Invoice to invoices, and so on, automatically. Also, add lovely methods like belongs_to and has_many to connect the entities, even at runtime if you’re so inclined.

We spent some time figuring out the proper entities and relationships we needed for our first couple of stories - not the best way to do it, we’ve been told later - and after a few hours of head-scratching when Rake started hanging for no apparent reason - a bug in test/unit-, we found that we could just run the test class as a regular Ruby script and started coding our first ever test case.

One particularly nice trick we used was to avoid naming join relationships like people_project, and coming up with a single noun to define it (in this case, slots). This way you can probably phrase your whole domain model in one or a few sentences, connecting all the nouns (“a project sponsor starts a new project and staffs its slots with people” would be an example). This simplified our domain language enormously, and communication is much easier. Also, we don’t have to spend more than two seconds coming up with a name for a variable, always a good thing.

And then, after less than 25 lines of model code, and 25 lines of test code, we had a green bar. Actually, a dot, but still, it kinda shows the power of this thing. Can you imagine what you’d be able to do in Java if you had just started using it and had only a couple of days to implement a story?