Incite Code

Blog of a Rails Developer

Using MacRuby to Write a .qif File Converter

I heard about the MacRuby project not too long ago and was quite excited about it for several reasons, the first is  that although it’s early days yet and version 0.4 doesn’t cover the full ruby spec, it’s showing signs of being a well performing ruby implementation. Others have already gone into significant detail about the differences, but to summarise: The original ruby implementation isn’t the best at memory management for long running processes, and this is the most recent of several other implementations that provide better performance. The other reason to get excited about this is that because MacRuby is essentially the ruby language written in Objective-C (not just a bridge), it means you can utilise both ruby’s syntactical sugar and the Cocoa library to write native Mac OS X applications using ruby code. This is pretty cool to me, because Objective-C has a pretty verbose syntax, manual memory allocation, and is generally not quite as pretty to read and write as ruby code.

Of course, nothing illustrates this quite as well as seeing how short a sample (trivial) GUI program is when written with MacRuby and HotCocoa:

example.rb
1
2
3
4
5
6
7
8
9
require 'hotcocoa'
include HotCocoa
application do |app|
  window :size => [100,50] do |win|
    b = button :title => 'Hello'
    b.on_action { puts 'World!' }
    win << b
  end
end

The equivalent code in Objective-C is about 3 times as long, less readable and much more confusing. That’s what I love about well written ruby code: It should be clear what’s happening, and straight to the point.

I couldn’t wait to try out writing a desktop app with MacRuby, so when a colleague needed some financial data converted from .qif format to .csv I had a look for a free tool to do this on Mac OS X, but couldn’t find one that fit the bill. So after utilising a bit of regex magic to convert the files, I decided to test out just how easy it was to write a desktop app in MacRuby, by writing an app with a GUI to do the file conversion process.

After trying out using XCode to create a new MacRuby project without much luck, I found that the HotCocoa library that comes with MacRuby (0.3+) can generate a blank app for you. Once MacRuby is installed (I’m using version 0.4) you can simply type

 hotcocoa myapp

from the terminal to generate a new application, with some laid out code, just waiting for you to add the visual elements and logic you need to your app. This sets up some rake tasks for compiling the application too using macrake (MacRuby’s binaries are just like you’re used to but prefixed with mac, macruby, macrake, macgem etc) including for when your application is finished, you can package up the MacRuby framework inside the final application for distribution, so that others can run your .app file without having to have MacRuby installed on their computer.

cd myapp
macrake        # This builds and launches the .app
macrake deploy # This builds a fully packaged version ready to ship

Overall, the process was pretty simple, I added some horizontal and vertical layouts to configure the window looked, and after only a few days of reading the documentation and coding in my spare time, I came up with Qif2csv. It’s a bit rough, and I still didn’t figure out how to make a check box with the ruby syntax, though feel free to download it, and browse the source. It was fun to try out something a bit different, and it’s always good to keep building new skills. Now of course, we can all anticipate the release of MacRuby 0.5 which is a largely performance focused release, and I believe will also support native threading.

Comments