Friday 27 July 2012

Sharing Ruby Programs

I hope that one theme that runs through these various blog pieces is a preference for simple solutions.

I wondered in another piece why it was necessary to go to the trouble of writing the Jquery-Rails gem just to distribute a few Javascript files?

I have often felt that it would be nice to be able to give someone else a copy of one of my Ruby programs. But it was always very obvious that the people I would be likely to give a program to would have neither the interest nor the expertise to get the MRI Ruby system working so that they could use my program.

JRuby changes all that.

Of course JRuby only works on a computer with the Java runtime system. But most PCs have it already and, if not, installing it is straightforward - unlike installing MRI Ruby and the Gems necessary for your project.

If you want to distribute your JRuby project (big or small) all you need to do is include the JRuby system within it and a shell script or batch file that sets the PATH variable and starts the application. And if you give a copy of the whole project to someone else all they have to do is load it onto their PC and double-click the script/batch file.

For a simple project the directory structure might look like this

.
├── jruby-1.7.0.preview1/
├── helloworld.rb
└── LocalJRubyTerminal.sh*

Or for a Ruby-on-Rails project it might look like this

.
├── app/
├── config/
├── db/
~
├── jruby-1.7.0.preview1/
~
├── public/
├── script/
~
├── config.ru
~
├── LocalJRubyTerminal.sh*
├── Rakefile
└── README

Unfortunately if you go looking for internet advice about making your JRuby app shareable you will find any number of complex solutions (Warbler, Rawr etc) and (as far as I can see) no simple solutions that take advantage of the Java runtime system.

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.