Welcome!

Welcome to the Thinking in Rails site.  This is a site where fans of the Ruby on Rails world can come and start to learn how to think in the pragmatic, agile, and DRY way that the Rails world requires.

This site (or at least the URL) was born out of a conversation with another programmer about the different ways that you have to think when you are a Rails programmer.  It’s not just the pragmatic principles, but the impression that there is a single “right” way to do a given task.

As a perl programmer who moved up the ranks from the original cgi-bin style pure perl scripts, then embedded into the apache server with mod_perl, and then the pseudo-frameworks of EmbPerl and HTML::Mason.  While there are perl web frameworks (Catalyst and MayPole for example), they just didn’t seem to…. feel right, or at least give the same “good feeling” that you get from looking at an elegant Ruby on Rails application.

To quote the Agile Web Development with Ruby on Rails book:

But there’s something else to Rails—something that’s hard to describe. Somehow, it just feels right. Of course, you’ll have to take our word for that until you write some Rails applications for yourself

I’m hoping this site will help people like myself who are new to Rails and Ruby to find a few bits and pieces of help to get them going on their way to being better and more productive Ruby on Rails programmers.

The Ins and Outs of Serving Rails

One of the biggest failures I found starting in Rails was the lack of a standard for hosting.  In the mod_perl or php world, the standard apache distribution has everything needed built in to the standard distribution, and you’d be hard pressed to find a hosting system (of any size) that doesn’t have them.

Rails on the other hand, seemed to have a boatload of ways to serve itself.  There doesn’t seem to be a “standard” webserver.  You have mongrel, nginx, phusion passenger (mod_rails), lighttp… the list goes on.  And it doesn’t seem that there’s an accepted “proper” way to do the webserving yet.  Maybe it’s a case of different tools for different cases, maybe it’s just a symptom of a system that’s relatively young (compared to the apache/perl/php world).

The Challenge of Learning Ruby

One of the biggest challenges I have as a Perl programmer is dealing with the change in the underlying language that Rails is written in, namely, Ruby.  To help with this I’m working myself through Project Euler to do a couple of things

  • Force myself to work through problems in Ruby
  • After solving the problems, see how others did them

The last item is the most important.  I generally start off in a very perl-ish, procedural way.  When the answer arrives it’s a real eye opener to see how others solve the issues.  For example, for one problem after solving it in what I thought was a fairly elegant way (a couple of short functions), seeing the first Ruby based solution in the forums the programmer had used a quick Monkey Patch (modification of the base Integer class in this case) to simply add a function that did what he wanted.  I had never even thought of this!  I knew the capabilities of Ruby to do this, but my brain just hadn’t triggered the idea that this was a good solution.

The pros and cons of Monkey Patching are a discussion for another day of course :)

I’m fairly proud of myself, I have worked through some of the problems and even just wrote the following line of code… on purpose!

(1..100).inject { |s,i| s*i }.to_s.split(//).inject(0) { |s,n| s + n.to_i }

I admit that my perl-ish nature of doing it all on one line came out a bit :) The actual solution that I came up with was a bit nicer of course!

Bought 2 Rails Books

Got a couple of Ruby on Rails books out at the local Chapters today. After hearing a bunch of reviews, I picked up “Head First Rails”. This has a very unique way of teaching, obvious not only from the front page of the book (which immediately said to me “get rid of this and go onto the next book, this is crap”) to the inside and first chapter I’m going through. However, give it a shot and read through the introduction chapter and give it a shot. It’s different for sure, but in a good way.  One of the biggest things for me was the first chapter wasn’t a “how to install rails” section.  It really gets right down to business, which is a nice change.

Also, on a whim I grabbed “Advanced Rails Recipes”. The latter one I had seen before, but the idea of just having a book full of “how to do random thing X” was fairly appealing.  I don’t technically need this just yet, but it’ll be nice to have a paper copy for when I do reach that stage :)

Discovering Ruby With Ruby Warrior

Thanks to the FV.rb last night I discovered Ruby Warrior, a neat and fun way of learning ruby.  It goes like this….

First, head to http://github.com/ryanb/ruby-warrior/tree/master and download the tree by using “git clone“, then simply run “bin/rubywarrior” out of the main ruby-warrior directory that the git clone command creates.  The first run will create a profile and set up the initial part of the “game”.

The “game” is a bit like the old adventure text games, with a simple ASCII “dungeon” that you see your guy move through, and as the levels progress you will encounter monsters, harder monsters, captives to rescue, and so on as you move from one side of the dungeon to the stairs on the other side.  The system is turn based, so you are basically creating yourself a little state machine.  You basically check to see if there’s something in front of you and if not walk, if it’s an enemy, attack, etc.

To do this you end up doing something like this:

  1. You look at the README file in the ruby-warrior/beginner-tower/level-001/ directory.  You may have “intermediate-tower” instead of beginner depending on the level you chose during the initial setup.
  2. Follow what the README file says and edit the file ruby-warrior/beginner-tower/level-001/player.rb adding code to make your warrior move and fight.
  3. Run bin/rubywarrior and see how well your warrior moves and fights.  It will either succeed and allow you to continue to the next level (in which case go to step 1, substituting the right number in the “level-00N” directory), or you will fail, in which case, re-edit the warrior.rb file and try again.

The game isn’t for complete and total programming n00bs, but might be a bit simple for people who know programming, but not ruby 🙂  It starts out with simple commands and simple if/then/else control structures.  However, it does make it cool and interesting enough that it has kept my interest for at least until now.

Quick Thoughts – Comparing HTML::Mason to Ruby on Rails

Since HTML::Mason is about as close as I’ve come to coding Rails before now, and Class::DBI is about as close to ActiveRecord as you can get in the Perl world (well, DBIx::Class is close too), I wanted to draw some comparisons for people.

  • The params variable is %ARGS
    • As with each request in Mason having access to the %ARGS hash, each view and controller in Rails appears to have access to the params variable with whatever parameters are passed on.
  • Helpers are Components
    • All those little “helpers” or scriptlets or whatever they are called all are pre-defined components. Putting <%= form_tag %> in your page is very similar to using <& /components/form_tag &> (if you had a bunch of pre-done components of course :)
  • ActiveRecord is Class::DBI or DBIx::Class
    • Already stated above. The same Zen of Class::DBI and it’s goodness is replicated fairly nicely in ActiveRecord. There doesn’t seem to be an obvious replication of some of the really cool stuff from SQL::Abstract (using [ ] arrayrefs and { } hashrefs to create complex and/or and nested queries). The “noobie” ActiveRecord that I’ve been presented with so far has allowed for this by allowing you to do something like Thing.find(:all, :conditions => ["age > ? and age < ?", agefrom, ageto ] ). Not as cool, and I’m sure that if anyone was reading this blog they’d chime in that all you have to do is use the SuperMegaAwesomeSQL-fu plugin and I’d get that functionality. It might also be in the next chapter of the book I’m reading too.

These are the big obvious ones I’ve seen so far, will update when I find more comparisons that help me wrap my mind around this stuff.