Monday 2 July 2012

JRuby and Java

While I have been using Ruby and Rails for various personal projects for a few years I have to confess I had paid little attention to JRuby. I guess I saw it as another flavour of Ruby designed to scratch somebody's itch to build a better mousetrap and by causing confusion would just dilute support for Ruby generally.

And Java - ugh! It looks even less user-friendly than C, if that's possible.

However recently I spent a bit longer delving into JRuby and the Java system after coming across a new programming language called Mirah - which is a way of writing Java code using Ruby-like syntax.

I feel a bit sheepish admitting that I didn't realize the Java system revolves around a Java bytecode interpreter and that Java programs are compiled to bytecode rather than to native machine code. Hence compiled Java programs (including the JRuby system) can run on any computer (Linux, Mac or Windows) that has the Java runtime installed. I have understood the concept of bytecode for years - it's just that I didn't associate it with Java.

Well this opens up a whole new perspective on JRuby and how to use it.

While Rails is a wonderful product for helping write web applications it used to be a real pain to get it working. You had to make sure you had the right version of MRI Ruby (not necessarily the version Ubuntu wanted to supply) and you had to be able to download and access the right Ruby gems. Just when I thought I had learned how to do this reliably along came a wonderful program called RVM (Ruby Version Manager) that magically does all the complex stuff, and does it even better than you would do it yourself.

But there is another way of looking at RVM - as a product that solves a problem that should never have been allowed to exist.

You can, of course, use RVM to manage JRuby. But installing JRuby is so trivially simple that there is no need for RVM. You just download the Zip file, extract it to whatever directory you like, add the jruby/bin directory to your PATH and it just works - whether you're on Linux, Mac or Windows.

What this also means is that you can have a separate copy of JRuby (the same or a different version) in each of your JRuby (or Rails) projects with all of the Gems specific to that project.

And if you copy the project (including its copy of JRuby) to any other computer it will work there just the same as on your PC. And this is also true of a Rails project - even though Rails with MRI Ruby has a reputation for being hard to deploy.

And if that's not enough JRuby can easily access the hundreds of existing Java libraries including, for example, all the bits needed to make a GUI program that works the same on Linux, Mac and Windows.

All credit to Matz for inventing the Ruby programming language. But JRuby is the only practical manifestation of it - and thanks to Charles Nutter for that.

No comments:

Post a Comment