Tapestry Training -- From The Source

Let me help you get your team up to speed in Tapestry ... fast. Visit howardlewisship.com for details on training, mentoring and support!

Wednesday, January 05, 2005

Playing with Ruby

So, I've been peeking a bit at Ruby in tiny fractions of spare time over the last few weeks. Ruby is a fully object-oriented, dynamically typed, scripting language. It's designed to be fast, simple and powerful. It has closures which are just amazing (if difficult to describe), and the lack of types makes many common programming patterns much less of a chore.

I first heard about it a year or two ago from Greg Burd (he's the kind of guy who just knows what's cool before other people do).

I didn't really give Ruby another look until recently. Dave Thomas really pushes Ruby on a (somewhat confused) Java audience at the NoFluffJustStuff symposiums. I too was resistant, but Dave really does a hard sell on Ruby ... how could I resist something that's "a dog's breakfast, but it works!". Dave sealed the deal by sending me a copy of his book (Programming Ruby, aka "The PickAxe", which suffered an untimely death-by-NyQuil on the way out to ApacheCon and had to be replaced).

In fact, with the rolling over of the new year, I found a good use for Ruby ... a copyright updater for the Tapestry and HiveMind source code. I had written one in Python a ways back, but it was limited to Java files.

What I wanted was something a bit smarter ... that would be able to adapt to different types of files (Java, XML, properties) and would be able to update the copyright message, rather than overriding it. That is, convert:

# Copyright 2004 The Apache Software Foundation
# Copyright 2004, 2005 The Apache Software Foundation

My trembling first journey into Ruby is this script, which does the work. It's sloppy, doesn't report errors well, and took me too long to write (almost as long as it would have in Java!) ... but it works and is impressively fast. In fact, I've been very surprised at just how fast Ruby is to load, parse and execute. Visibly faster than Python ... faster than Java I'd bet.

I may have to revise some of my comments in my upcoming TheServerSide Tech Talk (filmed last April).


Jamie Orchard-Hays said...

Welcome ;-) Now you have to check out Rails!

pate said...

After checking out Rails (an incredible web application framework) you should also take a look at needle (which is a rewrite of Copland, a ruby IoC system inspired by HiveMind).


ScottEllsworth said...

I am not sure I would change your comments to Paul Graham.

I have used a lot of languages like Ruby, Perl, Python, PHP, and the like, and while they are very useful for certain tasks, they are not Java replacements. They have a niche of their own, just like Java does.

Look at the task you used Ruby for - a one liner, which could be maintained or rewritten in similar times. Very efficient, much more effective than a similar amount of Java code, I suspect, but probably not something that your business life depends on keeping "maintained". I find that the ideal task for Ruby-like languages - small tasks that can be well understood and well expressed in a page of code. For those, they excel, because they are very terse, very effective, and you do not have mystery errors in a single page of code.

I find Java very effective for longer works, especially ones where I do not want to understand just what I did months ago, just how to make it Do The Thing It Does. Also, APIs with interfaces tend to react well to being coded in Java, as it supports the split between what I want to do, and the exact means I use to achieve that goal very well.

Aside: I must say, having done a lot of Cocoa in recent months, I do miss garbage collection tremendously when working in ObjC. I suspect that Ruby-like languages are going to start pushing for sensible auto-casts for Java-like languages eventually, but untyped languages, especially if variables are just created on the fly, can really bite you hard.

Java and database consulting for the life sciences

verbat said...

scott: don't fall in the propaganda trap. Ruby (like python or smalltalk) is definitely not an untyped language. And there is no variable autovivification, and a structured exception handling system just like java's.

"mystery errors" definitely don't happen in ruby more than NullPointerException's happen in java.
Btw, I love Nice (nice.sf.net) :)

Robert McGovern said...

Just wanted to say that your link to Ruby goes to the Python website :)


DJB said...


Just a little coding tip. The code where you're slurping all of the lines into an array can be replaced with IO.readlines.


- Dan

dh_honig said...

So I have also seen ruby hanging out on the block and I can't say that your code example inspires me to try.

After all the code you wrote appears it really can be achieved in 3 lines or less of perl...I have been pretty interested in groovy, for the fact it runs in Java JVM, which gives it cool applications...

Did you enjoy programming in Ruby? Reccomend it? It looks like a lot of effort for the simple operation it is performing.