Ruby

gnat on 2006-07-10T18:38:48

I'm so late to the game--matts and others were all playing with Ruby years ago, while I was just scheduling talks on it at OSCON. Now I'm finally getting my feet wet (ok, I'm jumping right in) and it's a blast. My guide: Ruby for Rails by David A. Black. He teaches Ruby a lot like I taught Perl--I keep finding echoes of my Perl classes in his explanations of references, closures, and so on.

As for Ruby, well it's true: it's like Perl only easier. I've been working on my coding chops, which have atrophied after several years of pencil pushing mindlessness at O'Reilly, and it feels good to be building up cortical muscle again. The first Ruby program I wrote was a maze solver. It turned into a depth-first search with support code to read mazes from files, turn x's and spaces into data structures, and so on. The learning curve here was realizing that when I was trying to do something complex and possibly impossible (pass to a function a reference to an accessor method) I should instead look at my code and figure out a better representation or implementation. I did, and lo! the code was simplified to the point where that messiness was no longer necessary.

The second program solved a problem from Dr Ecco's Cyberpuzzles: partition the integers 1-52 inclusive into four buckets such that no three integers satisfying a+b=c are in the same bucket. Again with the depth-first search (it's bringing home with emphasis why my AI prof seemed to use the term "search" synonymously with "AI") but this time I got an elegant representation of the problem, which made the implementation much easier. It's a great example of the rapid growth of the problem space: it's a nigh-instantaneous solution up to 40 integers, but give it 50 and suddenly it's taking tens of seconds to find the solution.

My current program is finding an Eulerian circuit in a graph. It's the Bridges of Königsburg problem: cross all the bridges exactly once. The mathematician Euler showed that it was impossible for the town of Königsburg, but a mathematician Fleury came up with the algorithm for finding the path if there is one. So I'm now writing Node and Edge classes with which to implement the algorithm, and again as I get more into the mindset, the more quickly the code comes. However, I've been sidetracked by reading up on the possible ways to implement graphs (adjacency matrices, etc.), which took me to eigenvalues and flashbacks to the Linear Algebra class I dropped in my second year at university, and .... well, you get the picture.

Anyway, I'm loving Ruby as much as I'm loving stretching my brain. What do I love about it? It has all the stuff that's missing from stock Perl (slurping in a file is trivial, as are filtering and reducing an array and several dozen other basic operations) and its basic premise that everything's an object makes it very easy to figure out what I should be doing to solve a particular problem. I'm not abandoning Perl (I began the rehabilitation of my brain by reading Wikipedia's description of quicksort and then implementing it in Perl) but I'm enjoying exercising my brain by learning new oddities (e.g., closures) and contorting my powers of expression in new ways. In short, I'm having fun doing something at the keyboard that isn't email.


Welcome to the Red Side, Luke

djberg96 on 2006-07-11T02:22:24

Glad you're enjoying Ruby. I think you're going to become very spoiled with it, very quickly. I know I was. :)