I’ve been using Ruby and rails at work for about a month now, and I have to say, it’s really growing on me.
From the beginning, I was amazed at how much you can do with so little code. It’s been a month, and I have some impressions. They’re not unique (it seems that everyone is playing with Ruby recently), but they’re mine.
First off, I love the terseness of the language. It’s amazing what you can do with one line of code, compare:
Java:
Iterator i = list.iterator();
while ( i.hasNext()) {
Object o = i.next();
if ( o == null ) {
i.remove();
}
}
Ruby:
list.compact
Granted, that might not be the best example, since compact is built in to arrays in ruby, however, this does the same thing
list.reject{|element| element.nil?}
And the amazing part is that you get this type of reduction in total lines of code everywhere. Since we’re developing a web based application, we’ve been using Rails as well. Rails brings Ruby’s speed of development to the web. Coming from a Struts based application, it’s really shocking how quickly you can create a model, view and controller. This speed benefits your architecture as well –if it’s easy to make a new MVC, then you’re more apt to do it, rather than adding additional code to one that’s already there.
Rails does have its faults, and they impact both the speed at which you develop, and the Object Oriented nature of the code you write.
First off, ActiveRecord, Rails’s persistence mechanism, uses single table inheritance. This doesn’t seem to be such a bad thing at first –after all, Hibernate, OJB and just about every other O/R layer offer this feature as well. Where ActiveRecord breaks down is that it doesn’t have a mapping layer. Instead, all objects are built directly from database metadata. Most of the time, this works very well, as O/R layers generally map an object directly on to a database table, and ActiveRecord saves you this tedious and error prone step. However, if you use single table inheritance, that database table has the superset of all methods available for all objects in the inheritance hierarchy. This means that objects gain methods that they shouldn’t have, and there is no way to prevent them from having these extra methods. This is truly bad for object orientation, as you can’t define exactly what methods your objects expose. Sure, you can ‘know’ what method each type exposes, but that’s akin to sticking your fingers in your ears and screaming “I’m using OO programming techniques!!! LA LA LA!!!”, which, as we all know, doesn’t mean you’re programming in an OO manner. I’d suggest being able to tell ActiveRecord which methods an object should receive, like this:
class Article
methods :title,:description,:text
end
class NewspaperArtice <Article
methods :byline, :run_date
end
class MagazineArticle < Article
methods :author,:magazine_title,:issue_date
end
In this example, the database would have the colums title, description, text, byline, run_data, author, magazine_title and issue_date.
Second, the documentation is atrocious. Being a java programmer, I’m used to excellent documentation. JavaDoc is a godsend for programming, and Java’s documentation is thorough, complete and correct. Even the open source Java projects have continued this tradition. Sadly, it’s not so in the Ruby world. Part of this comes from Ruby’s dynamism, it’s impossible to tell exactly how objects have been changed in any execution environment. How, for instance do you communicate that Ruby on rails added several methods to numeric objects? However, most of the documentation is just inadequate. I’ve looked extensively at the rails docs, and have yet to find a good explanation of how exactly I can configure routes.rb. this is no help. Where’s the documentation for the rails request object? Where is the testing documentation? Sadly, Google is more of a help for figuring out what to do in rails than any of their documentation. However, docs are an easily fixable problem, and I expect them to improve rapidly, now that rails has a high profile.
Still, ruby and rails are extremely valuable tools to have in your belt, and even with the above flaws, are worth using. The productivity increases are amazing. I’ve developed two projects in rails so far, one for work and one for a family friend. The project for work went as smoothly as any I’ve ever been on, and –get this– the one for the family friend was completed in the two week window that I was waiting for a design firm to re-do the mockups to use CSS. The original java implementation took months.
Yeah, it’s nice to be able to code a web app in the same amount of time it takes brain dead designers to do CSS.