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.
Everything is an object and lot of functions for string manipulation appear to be lifted right from JS, for example
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:
This can be a real pain in the ass in Coldfusion, but it’s great in Ruby:
starttick = line.split(",").split(" ").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!