Monday, March 31, 2008

Ruby on Rails after 3 days

I've been learning Ruby on Rails at work for the past 3 days. I had never really worked with Rails before, though I had certainly read enough to form misguided opinions about what was good and what was bad. After some hands-on experience, I have a better idea of what I do and don't like.


Routes are Rails' way to find the handler for a given request. You can establish routes by editing routes.rb. I don't know whether this is common practice, but an advantage of using a Ruby source file over XML is that you can use Ruby language and library features. This makes it easier to add conditional or generated routes, something that the Rails REST support does with abandon.

Routes are also 2-way. They are also used when you want to know the URL to a particular controller/action. This makes virtually impossible for your URL recognition and URL creation code to get out of sync.


RJS is Ruby's javascript-generation library. It's used as part of infrastructure for AJAX applications. Most of the Ruby AJAX tutorials that I've seen do something that I consider to be very bad practice - they run code on the server that doesn't need to be. The typical pattern seems to be to use an RJS helper method to emit Javascript that attaches a listener to some other HTML element. This listener then issues an XmlHttpRequest to the server, which produces Javascript code. This code is then sent to the client, where it is eval()'d. While there is nothing wrong with that pattern per se, imagine doing that for something as simple as clearing a text box. Imagine thousands of clients contacting the server just so that they can clear their text boxes.

Of course, in typical Rails fashion, there are many ways to use RJS. Rather than use it in response to a lightweight request to the server, you can also use it when you are building up the original HTML page. This can both simplify your application and also increase its perceived performance. Of course, you need to know that you are able to do this.

Unfortunately, the Javascript that is generated isn't that much different from the original Ruby. Compare this RJS Ruby code:
page['query_results'].value = ''
to this hand-crafted Javascript:
window['query_results'].value = '';
I'm generally against code-which-generates-code, and especially when used to avoid learning a new programming language. Web developers usually end up developing a reasonable understanding of HTML, and those in the Ruby community seem to also be reasonably proficient with CSS. I would hate for them to miss out on a fantastic language like Javascript.

Ruby Itself

Finally, Ruby as a language creates some interesting situations. I find its dynamism to be both a blessing and a curse. It's pretty amazing that Aptana Studio seems to be able to "go to declaration" reasonably well. Of course, this only works for methods which are defined statically, in the source code. Some methods are added dynamically (via method_missing or some other black magic of which I am unaware). I believe that it is impossible to even find out what these are - you simply have to know what methods ActiveRecord may have added to your model objects. I'm sure that this gets easier with exposure.

Incidentally, this very dynamism has spawned some very interesting workarounds. Since the routing tables are stored as data, it's hard if not impossible to see the set of routes by looking at the code. However, there is a rake target to see the contents of the routing table:
rake routes
Similarly, the migration nature of Rails database management makes it quite hard to quickly grok the structure of the database. However, Rails generates a schema.rb file which theoretically mirrors the current state of your database schema. Perhaps not strictly necessary, but awfully convenient.


In all, I'm reasonably happy with Rails so far. It's quite different from previous experiences that I've had developing web applications. In a way, it hearkens back to when I was first learning HTML. It was trivially easy to write some HTML and then see what it looked like in a web browser. The code/test cycle was very small. Rails brings the same sort of simplicity to web application development. Rather than spend time writing code, you spend time learning how to write the code in the correct Rails mindset.

I don't know if Rails will scale well. I don't know if Ruby will be able to keep up. I don't know if ActiveRecord will strangle us or liberate us. However, I do know myself, and I know that I will stay skeptical for quite some time. Hopefully, one day I will realize that I've been happily writing Ruby code for years and find that neither it nor Rails have failed me.

No comments: