Ducking and Dodging with Rails

One of the other major advantages of where I am currently living is the fact that I am never short of new problems to try to solve with code - with my gracious hosts being heavily involved with a variety of different types of research on the Internet, I am often able to glance over and find a new problem to be solved.

From my perspective, this is great. It gives me a chance to try out some of the Ruby I've been looking at with (usually) fairly simple problems. For example, one of my "tasks" was to spider over 500 or so linked pages (I later found it was about 90Mb worth... On dial-up, none the less!) and get the contact information for the professionals listed (before anyone asks, no, not to spam them - we're all above board in this establishment!).

It is in these small applications that Ruby really shines. The fact that it's so light in the syntax department means that after 10 minutes or so, the task is complete. Although realistically I am not quite sure if PHP would have taken much longer in many of the solutions, the code always appeared cleaner than I could ever have hoped for in PHP. Even just the ability to do something like-

 open("") {|f|
    f.each_line {|line| p line}

rather than have to stress about remembering to close handles and such in case of errors is very nice (and let's not even talk about if I had to use cURL or somthing...). Operations which are somewhat atomic can be treated atomically - The source is opened, read and closed.

Anyhu, this tinkering to solve problems has also given me a good excuse to start playing with Ruby On Rails for a real application. The application was started back when I initially raved on about Rails, however due to "real work I haven't had much time to spend on it. It's nothing major, new or exciting (and the colour scheme taken from the (currently on hiatus) main Military Minded website looks like ass...), but it's been an awesome chance to come to grips with the framework. Although I haven't been keeping strict tabs on it, I'd suspect I've spent a total of maybe 6-10 hours of work and research getting this initial version running - That includes the time to eat the pizzato become acquainted with the whole framework whilst building it.

All that I can say is "WOW!".

In most other projects, we've been using some homegrown MVC-esque frameworks to abstract out most of our dirty work in PHP - classes to handle forms for us, classes to abstract off most of our page handling, code to build menus dynamically from the code as we manipulate it. Whether a testament to our lack of vision or whatever, nothing we have rivals Rails when it comes to being lightweight and powerful. Perhaps that's because we've only abstracted away "just enough" to make our lives simpler, who knows.

Issues which would normally require a fair amount of coding, such as user authentication, took less than an hour with the help of a Wiki based HOWTO. Form validation (which would normally be fairly time consuming to set up for all the different field types and the such) is a matter of minutes. The best bit of all of this is, of course, that if you don't require validation, that is fine - you don't have to have placeholder functions returning true or do any magic to turn validation on. It just works.

Of the whole time I spent writing the application, I believe I spent more time researching how to do things, rather than actually doing them.

In fact, the most complex bit was done this afternoon - getting Rails working under Apache 2 with FastCGI. Lacking official FastCGI support from Fedora (first disappointment), with the tarballs installed I was still having no luck getting anything but 500 errors. Eventually though, with a little help I was able to get it running. Although the title is a little misleading, the steps should be right for any UNIX based system (and certainly were for me).

So, my first real Rails app - lets see how they likes them apples...