Ruby turns you into McGyver

Having a scripting language in the toolbox is invaluable; and if the scripting language happens to be Ruby, you’re armed with the Swiss knife that’ll turn you into McGyver.

As a Java developer, you need to know more than Java to be able to work efficiently:

  • you need to know an editor like Emacs or Vi,
  • you need to know a fair bit of Linux/Unix commands, and of shell script,
  • you need to know a scripting language.

Why the scripting language? Because there are lots of stuff you need to do on a daily basis, and Java is far too big a tool to do them (understand, far too slow to code, far too slow to compile, far too slow to deploy, etc.):

  • build scripts,
  • deployment scripts,
  • generated scripts via regexp (like creating SQL delete from SQL insert),
  • scripts to modify a group of files,
  • scripts to check the state of the JVM,
  • etc., etc., etc.

Ant (or even more pathological, Maven) uses XML to alleviate some of these issues, but XML is just not “appropriate” for these tasks: the build scripts size explodes and are very hard to maintain, unless you resort to some macrodef trickery that are just symptomatic of the fact that you actually really need a proper scripting language…

The two recent examples I have is actually producing reports in a quick fashion. The first example is actually something I wrote, as I was asked to create a list of users of an application by querying the LDAP directory. On the very first day of work in my very first company, I worked with LDAP, and have been ever since, so doing an ldapsearch for this kind of query was easy stuff; formatting the report is always a bit more tricky, because you have to sed the hell out of the output, and let’s face it, I don’t speak sed very well. The Java/JNDI option is far far too heavy for a simple report (and it does make you look a bit stupid if it takes you like half a day to produce a report listing all your users…), so the scripting option is ideal.

Having never used the ruby-ldap extension, I was a bit worried that its installation would be hard, especially behind an overzealous proxy, but it went very smoothly on Cygwin — here are the command in the unpacked archive:

$ ruby extconf
$ make
$ make install

Once installed, the script itself was a pleasure:

require 'ldap'

ldap_host = 'ldap_server'
ldap_port = 389
ldap_base_dn = 'dc=myformercompany,dc=ie'

def print_entry(entry)
  puts "#{entry.vals('uid')},#{entry.vals('givenName')}, #{entry.vals('sn')}, #{entry.vals('mail')}"

puts "uid, first name, surname, email"
ldap_conn =, ldap_port)
ldap_conn.bind('cn=Directory Manager', 'xxxxx') {, LDAP::LDAP_SCOPE_SUBTREE,
                   ldap_filter, ldap_attrs){ |entry| print_entry(entry) }

All put together in 5 min. The cool thing about this was that the people who were requesting this report asked for different versions with different attributes, and the turnaround was almost instantaneous!

The second example is a script I came across whilst browsing snipplr, which solves problems I have seen solved with XML/XSL in the past: extracting the substantifique moëlle of an XML report to produce a summary on a CI report. Using Nokogiri, it just becomes trivial to parse an XML document (here a JUnit XML log) to produce a report. Think of all the applications! Creating code coverage reports, creating dashboards, providing querying features to end users via DSLs (piece of cake with ruby)…

Ruby (and more specifically JRuby) has really become for me one of the most useful tools, and it has that extra-special feature that it makes me feel like McGyver whenever I come up with a solution to a problem within minutes in a very minimal amount of code. And Holy Richard Dean Anderson knows that if the following were to happen, life would be even happier:

  • Rakify all Java projects, and ditch Ant (Maven),
  • Create a Ruby implementation of Emacs, where you can write Ruby scripts rather than ELisp,
  • Write a Ruby version of eclipsemonkey (I know the project is actually parked, but I just cannot. understand. why, the JS version being already so absolutely essential) — Aptana may have that in their drawers, actually, I never really checked.

And there you go, I just cannot resist: