Dear fellow Ruby developers, please kick the habit of catching and rethrowing exceptions, thus ridding your users of the expeptions’ stack traces. Recently I had to patch the source code of an ActiveRecord adapter as well as the underlying DBI implementation in order to find out where one particular exception was originating from. This makes debugging code really a hassle. I haven’t had this kind of problem in Java since nested exceptions.
01
Aug 08
Scripting The Night Away
One of the very nice features of Mac OS X ist the ability of almost every reasonable application to receive AppleEvents, which makes the whole OS-app-bundle highly scriptable. Normally, Apple recommends that the scripting is done using a malevolent, programmer-unfirndly language named “AppleScript”. But there are alternatives, e.g. using Ruby together with the rb-appscript library.
As a first try, I wrote a small hack solving the following problem:
In Aperture, I normally sort photos in folders named YYYY_MM_DD, so when I sort them by alphabet (or Aperture sorts them and I cannot change the sort order), the oldest ones show up first. Unfortunately, iPhoto imported photos from my camera in the German format DD.MM.YYYY, so the alphabetical sorting is useless.
The ruby script goes like this:
require "appscript"
include Appscript
app = app(‘Aperture’)
puts app.projects
app.get(app.projects).each { |project|
name = app.get(project.name)
if name =~ /^\d\d\.\d\d.\d\d\d\d$/
day = name[0, 2]
month = name[3, 2]
year = name[6, 4]
app.projects[name].name.set(year+"_"+month+"_"+day)
end
}
23
Jul 08
Documentation Note
If you are documenting Java methods, try to fit everything really important (like constraints for the method) into the first sentence of the javadoc. This sentence is displayed along wiht the overview of the class method.
Everything after the first sentence vanishes in the distance where no-one will ever read it.
12
Jun 08
Name That Code

Created by OnePlusYou
I named those codes, and boy, what a miracle – one hundred percent right. Try it yourself!
10
Jun 08
The Ultimate Code Browser
I want a Firefox Plugin that recognizes that the page I’m looking at contains Java, does syntax highlighting and, most of all, inserts links to the classes and methods referenced in the code. The plugin (or a server it can ask) should know where the source code of a class like org.springframework.beans.factory.BeanFactory can be found.
First I thought that such a plugin would need (or could at least profit from) sem***ic web technologies (no dirty words on this blog, please), but it probably wouldn’t. All we need is a global registry that maps package names to the URLs of the web interfaces of the respective source repositories. (Maybe Google Code Search could be such a repository, but its results are often pretty bad.) The plugin would also have to include a reasonably good Java parser, and maybe a heuristic to distinguish betweeen Java and non-Java text.
Why would I want such a thing? Well, I often have to dig through the code of some open source library in order to understand a bug, or just to figure out what I can do with it, and how, because the documentation is incomplete or wrong. Usually I create an Eclipse project and import the code from the source repository, but that’s quite a lot of overhead if I just need to browse the code for ten minutes or so. On the other hand, digging through source code on web pages, without the help of links between the pages, is just a nuisance.
And wouldn’t it just be so cool to have one of the most important features of an IDE as a browser plugin, enhanced by the power of the world wide web?
P.S.: Or maybe I don’t want a browser plugin – maybe I just want Google to put some more effort into Code Search? Come on, guys, you’ve got all it takes – the global repository, the parsers, the heuristics…
03
Apr 08
Use quote_value in Rails 2
This is just a note to all who, like me, wonder why the heck ActiveRecord’s quote method in model classes doesn’t work anymore in Rails 2.x: quote seemingly has been replaced by quote_value, though this is not listed on the deprecation page. So if you get an error that says undefined method `quote’ for your model class, replace the calls to quote by quote_value.
28
Jan 08
Introduction to Algorithms
Maybe this is old news to some, but I recently discovered that there’s a complete recording (audio at iTunes, audio and video on the web) of an MIT lecture by Prof. Charles E. Leiserson using “The Big Book” Introduction to Algorithms (which he co-wrote) as textbook. I just listened to the first of 25 lectures and liked it as a refresher course on the stuff I learned at university years ago. Lecture notes are available online as well.
25
Jan 08
ActiveRecord and Hibernate – Friends or Foes?
Currently, I am in the nice situation to decide for myself with which framework I will build a demonstration portal. So, as I want to profit from the fast-paced development processes (at least the Ruby aficionados say so) and the lightweightiness of Rails, I decided to go the Rail(s)Way.
Problem is, that I have to use a legacy database in PostgreSQL where I am not allowed to tinker with the structure, but I am forced to insert data into the tables. The database was created using Hibernate, which means an overall database sequence is used to generate the unique IDs. Unfortunately my new pal ActiveRecord does not know anything about global Postgres sequences, so I had to figure out a way to use the hibernate sequence in a hack to get my unique IDs.
What I did was to use the hook before_validation_on_create in the ActiveRecord table model to create an ID. This looks like this:
[ruby]
class Term < ActiveRecord::Base
before_validation_on_create :generate_id
private
# use the hibernate sequence to generate the next id
def generate_id
self.id = Term.connection,execute(“select nextval(‘hibernate_sequence’)”).result[0][0]
end
end
[/ruby]
11
Jan 08
Developer Coffee Mug
I need a coffee mug with all the nifty regular expression constructs on it. Especially the java character classes would be nice to have.
02
Nov 07
My Favourite Programming Font
When it comes to editing source code, Bitstream Vera Sans Mono is the one true ghod. Consolas is too fat, Courier has disturbingly many serifs. Monaco is for brainwashed Apple disciples who think using a Comic-Sans-like monospaced font is sort of hip.
More detailed reviews of programming fonts over at Coding Horror.