Ryan Carmelo Briones

mostly harmless

Rails is not Ruby for Beginners August 27, 2010

There, I said it. It needed to be said.

But Rails is easy!

This is not to say Rails isn’t easy to use. Rails seems a lot easier than doing some equivalent work in JavaEE or .NET. I mean, you can create a blog 15 minutes and you can deploy your application to Heroku in the 5 minutes after! Yes, it is true that you can achive quick results developing your first Ruby web application using Rails and even be decently successful in the long term. What I’m suggesting is that Rails as a beginning step is not conducive to mastery of the Ruby language or web development.

Rails is a series of tubes abstractions

First of all, Rails is a slick collection of web application abstractions and configuration DSLs. At a high level Rails includes:

Herein lies the most of the magic that makes Rails as easy as it is. This is why I came to Rails in early 2006. I had been writing Perl and PHP web applications/scripts using CGI mapped to subroutine dispatch tables and working with weak data abstractions; Ruby on Rails felt like the second coming for me. With it’s use of the MVC pattern and easy to use Object-Relation mapper Rails offered a solid framework for simplifying the database-driven web application. But because I’ve had to solve a lot of these problems in the past writing code myself I have a good idea when Rails’ abstractions aren’t the right tool for the job or how to work around their assumptions when they are wrong for my problem.

For the intermediate or advanced Ruby programmer, and in general when facing an abstraction you don’t understand, an exercise to overcome a lack of understanding how a library or framework handles things is to take time out to read it’s source code, if possible. Digging into Rails internals to try to figure out what’s going on is one of my favorite things to do. There are some good nuggets of Ruby goodness hidden deep in the Rails source. Unfortunately I can’t say the same for beginning rubyists and beginner programmers.

Rails is also a series of hidden Ruby extensions

For Rails to accomplish what it does, in both public and private APIs, Rails adds quite a bit of extensions to the Ruby core language and standard libraries. While this is a very natural progression for language, frameworks and applications it creates an issue for newcomers to the language; “Rails Ruby” becomes tantamount to magic and creates a steep learning curve for Ruby beginners looking to “use the source” as a learning experience. Prime offenders include Symbol#to_proc and alias_method_chain.

Again, these extensions are also abstractions of applied experience from Ruby applications and writing frameworks over time. These abstractions are useful tools to the practiced rubyist and should be studied as such.

Where does one start then?

I hope to write about this more in the future, but if you want to get started with Ruby writing web applications I suggest going as bare metal as possible. In my presentation “Ruby for the Web”, I suggest practice with writing Ruby CGI applications. Focus on the HTTP part of web programming; get a feel for how your application interacts with the request and response. Inspect the value of ENV over and over. Learn how to turn your procedural CGI “scripts” into Object-Oriented web applications. Read the HTTP RFC. Learn to write tests for your code; even better TDD your applications. Practice, practice, practice.

Then look at Rack. :)