Ruby in anger

Ruby

I finally had a chance to use Ruby for a legitimate work-related task today. I quickly used the Ruby Windows Installer to install Ruby – it even sorted out my PATH variable and file associations so *.rb files run right after installation. Nice.

I needed to parse a very large custom log file (10MB+) for my sins, and I needed to do lots of string parsing.

Coming from a Java and Coldfusion background in terms of server-side programming, programming in Ruby is much more like programming on the client-side, in JavaScript. Not the same, just similar.

Everything is an object and lot of functions for string manipulation appear to be lifted right from JS, for example split() and length. Having used Prototype JS, the extended function for string replacement called gsub() even makes an appearance in Ruby, although I’m pretty sure that function was ported from Ruby to Prototype JS, given the background of the Prototype JS crowd.

So Ruby is very different to Coldfusion. There is lot to like, for example:

Function Chaining

This can be a real pain in the ass in Coldfusion, but it’s great in Ruby:

starttick = line.split(",")[5].split(" ")[1].gsub('(', '').gsub(')', '')

Lots and lots of functions

The API is vast, just check out the 75+ Array functions!

No tag syntax

The code is neat and concise:


# Get max
if (difms > maxdif)
  maxdif = difms
end

# Get total (for avg)
total += difms

Lots and lots of examples

Ruby is popular, no doubt about that. And if you get stuck, like I was today wondering how to access command line arguments, a quick Google will often sort you out.

My main peeve in fact was the way that the style guides all suggest using 2 spaces for indentation instead of a tab. This just seemed awkward to me.

I’m looking forward to trying other things with Ruby, and perhaps some of these won’t be as simple as in Coldfusion (such as database access). We’ll see!

Advertisements
This entry was posted in regular and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s