My First Time Mining Ruby

Posted: Sun, 20 November 2005 | permalink | No comments

I'm not usually a grand fan of the latest and greatest ``fads'' in programming. The only C# I know is the black key next to D#, and I loathe coffee. An IDE in the hands of a poor programmer will still result in crap code. While I do think that agile methods (such as XP) can do well in certain development environments, I certainly don't subscribe to the notion that XP can solve all the world's software production ills. And so on.

I do know quite a number of programming languages, and tend to pick up new ones out of intellectual curiousity (although Ada did put a severe dent in that habit) or because a bit of research tells me that the language might be particularly suitable for a particular task (although I've since learned that the actual purpose of PHP isn't dynamic websites so much as bringing the security levels of Linux boxes in line with Windows-led industry standards). I also learn languages because I want to hack on projects written in them, which is motivational, but not a particularly good way to get a deep understanding of a language.

Why all of this introspection in a blog? So you have the background to understand how everything about Ruby is different for me, including how I'm getting started using it. I have no interest in hacking on a Ruby-implemented project, and -- although Rails proponents give me an impression similar to that of a horny teenager with a shiny new unlimited broadband account -- I don't have any particular use for Ruby at the moment (I want to write less webapps, FFS, not more!).

So, why the hell did I fork out $90 hard-earned South Pacific pesos for a copy of "Programming Ruby: The Pragmatic Programmers' Guide"? A couple of reasons:

I'm glad I got "Programming Ruby". The book is superbly organised, reads very easily, and gives you all of the love as and when you need it. Surprisingly for me and a technical book, I read the first section of the book "cover to cover". By the end of it, I came away with a real (if naturally superficial) understanding of the language, and a feeling that I was ready to tackle the world in Ruby.

It is a sad state of affairs when the first piece of new programming you can sink your teeth into is some four days after finishing your initial studies into a language, but this afternoon I wrote my first Ruby program in anger. It's quite simple -- just take stdin and rewrite it all on stdout with the lines in a random order. But I wrote it in a new programming language, in about 3 minutes, with nothing more than ri (Ruby's answer to firing up python and typing help(xyzzy), more or less) and "Programming Ruby". I think I also produced a suitably reasonable result:

#!/usr/bin/env ruby

lines = []

while line = gets() do
	lines << line
end
	
while ! lines.empty? do
	idx = rand(lines.length)
	puts lines[idx]
	lines.delete_at(idx)
end

(Any Ruby afficionados out there, feel free to e-mail me and tell me what egregious sins I've committed).

I think it looks reasonably neat and clean, it's certainly simple, and the use of real Object Orientation makes for a certain readability (I think the method call lines.empty? is particularly cute, although I did initially consider the use of a question mark in a method name to be a crime against humanity).

So, will I be using Ruby more in the future? I think so. There are some features in Ruby which I'm hoping to get a lot more cosy with -- the idea of "blocks" looks particularly interesting, and I don't think the utility of languages that have been designed, top-to-bottom, to really live the OO ideal has been explored enough (you SmallTalk bigots can stop glaring at me now -- really, I mean it -- stop it!).

It's far too early to tell whether Ruby will become my "go to" language for random development tasks, but anything that stops the nightmares (where I start thinking about using PHP as a general scripting language) cannot be all bad. And, if I continue to make new webapps, Rails will probably come in handy.

Update: HTML <pre> tags don't like having < inside them unescaped. Also, Philipp Kern pointed out that the Ruby documentation browser is called ri (not ir as I originally wrote). Philipp was also kind enough to provide a much shorter version of my original script, which shows the power of Ruby so much better:

 lines = []
 STDIN.each_line { |line| lines << line }
 lines.sort_by { rand }.each { |line| puts line }

Now tell me that isn't a language worth using!


Post a comment

All comments are held for moderation; markdown formatting accepted.

This is a honeypot form. Do not use this form unless you want to get your IP address blacklisted. Use the second form below for comments.
Name: (required)
E-mail: (required, not published)
Website: (optional)
Name: (required)
E-mail: (required, not published)
Website: (optional)