Ruby on the Client Side

Posted by Burak Kanber | Comments

A Google search for cross-platform development languages results in the usual suspects: Java, Python, and C/C++. We needed something a bit more agile for the newest project we’re working on, and decided to start from the ground up with Ruby. We’ve discovered that with the right tools and some ingenuity, it’s actually a wonderfully efficient tool for writing client side applications.

I’ve been computer programming for about 7 years as of this writing–I got my start with Perl, and moved on to ASP and PHP for server side development. I learned Microsoft’s Visual Basic for Windows client programming, and then began using the Ruby on Rails platform exclusively for web programming.

If you don’t know about Ruby on Rails, then I strongly suggest looking into it.

We realized that our new web service (which has a client-side application as well) should support the 3 major operating systems: Windows, Linux, and Mac OS X. A Microsoft technology works great for Windows: VB.NET, VC#.NET and VC++.NET are all wonderful solutions for Windows programming. But these aren’t supported too well on Linux or Mac. The C family works well on Linux, and Mac OS really only uses proprietary languages. Java seems to be the only “true” cross-platform language, as it runs inside a virtual machine, but I don’t know Java and ran into problems while trying it out (namely compatibility issues with Swing, the standard method of creating Java GUIs). My stomach dropped as I realized I would have to write three different copies of the same software.

Until I ran into rubyScript2Exe. RubyScript2Exe is a Ruby compiler. You can write a program in Ruby, “compile” it, and distribute it as if it were a Windows executable or Linux/Mac binary.

Ok, so it doesn’t really compile your Ruby program. Instead, it packages it into an executable along with the Ruby language itself and all your required libraries/files, and includes a just-in-time interpreter (or something like that–read the manual for details on the mechanics of it all). The end result? An executable file that Windows or Linux or Mac users can double-click on–your software just runs. It’s a beautiful thing.

But what about a GUI? Ruby is primarily a command-line interface. There are GUI tools (such as Tk) for Ruby, but these are restrictive and somewhat complicated, especially when you need quick results with a minimal UI.

So, where to next? I took Google Desktop’s user preferences as an example. From what I remember (it may be different now), your user preferences popped up in a browser window. I decided to do a similar thing.

Ruby is such a wonderfully fun, powerful, and fast (on developement) language to use. I had a basic user interface “engine” up and running in Ruby in under an hour. Using the ‘socket’ class in Ruby, you can easily create a basic “web server” that listens on a port on your localhost, and can respond to requests. Also, using your OS’s shell API extensions, you can make the default browser pop up with your software’s main page loaded when the program is started.

What we ended up with is 5 Ruby classes: one class that serves as the “core” of our software–this code contains absolutely no operating-system specific code and serves as the superclass of all the others. We 3 OS-specific classes: a class (child of the core class) each for Windows, Linux, and Mac. These ancillary classes contain only one or two methods, specific to the OS it was meant for. Our last class just ties up some loose ends, and is specific to our application.

Packaging with RubyScript2Exe is a breeze as well. Make sure to use the –rubyscript2exe-rubyw option so that no command window pops up when you start your program. Executable size is reasonable–in no case is it greater than 3 MB (remember, that includes all your Ruby, all the Ruby interpreter you need, and all the libraries you need, as well as the just-in-time environment).

So far, our software is tested extensively on Windows and Linux (Ubuntu), and works pretty much perfectly. No complaints here. We haven’t had a chance to test on Mac as much as we’d like, as neither of us own one! (You can always donate one…)

The best part about all of this? Writing your code only once, and having it work without hitch on multiple operating systems. I really owe Erik Veenstra (writer of RubyScript2Exe) a big one for this–he’s cut our development time in three.

Another great advantage of this is that you can integrate your client-side software and user settings into your web site/application. This gives the user only one location for settings to worry about.

What a relief.

Comments

No comments have been made.    post comment

Post a comment