JRuby
There is more than one way to experiment with [Ruby](index.html.) JRuby is a rapidly maturing version of Ruby written for the Java Virtual Machine. This gives you a great deal of platform independence, since JRuby will comfortably run anywhere that Java runs. It also provides you with access to Java's huge standard library. I thought I would take a little time to examine the Jruby implementation, which is nearing a 1.0 release.
We need Java before we can do anything with JRuby, though. I already have 1.6.0 installed on my machine. If you don't have Java, now is the time to [get it](http://www.java.com/en/download/index.jsp.)
With Java safely installed on our machine, it is time to download and install JRuby. The [download](http://www.java.com/en/download/index.jsp) is a simple archive file - most Windows users will want to go for the zipped version, since that is best understood by their system. I grabbed the tarred and gzipped archive of the binaries for myself. An automatic installer would be nice, but I suppose it can wait until the maintainers feel that the language itself is ready for the world.
The archive contains a bin folder
containing several interesting files. The ones which
interest me most are jruby,
jirb, and gem. Good, supporting
[Ruby Gems](http://rubygems.org/) means that we can
install from the standard Ruby repositories. I need to
put this folder at a sensible location on my system, and
then put that location on my path.
$ sudo mv jruby-1.0.1/ /opt/jruby $ export PATH=/opt/jruby/bin:$PATH $ which jirb /opt/jruby/bin/jirb
I got that right, so I can safely add the export
PATH line to my $HOME/.bash_profile
when I feel like it.
There's probably a quick way to do it in Windows as well, but here's what I know how to do.
- Open the Control Panel
- Select "System"
- Select "Advanced system settings"
- Select "Environment Variables" button.
- Select "Path" from System Variables if you have admin privileges, otherwise from User variables.
- Click "Edit".
- At the beginning of the "Variable value"
field, put the location of your
jruby\binfolder:C:\jruby\bin;C:\Tcl\bin;C:\Ruby\bin;%PATH% - Click "Ok" until all those lovely dialog boxes go away.
Now you should be able to access the JRuby commands
from any console1. I want to make sure the Java
interface works, so I'll fire up the
jirb shell and test with a quick Swing
"Hello World" dialog borrowing from the Hello
World
Repository:http://www.roesler-ac.de/wolfram/hello.htm#Java-Swing.
$ jirb
irb(main):001:0> require 'java'
=> true
irb(main):002:0> include_class('javax.swing.JOptionPane')
=> ["javax.swing.JOptionPane"]
irb(main):003:0> JOptionPane.showMessageDialog(null, "Hello World!")
=> nil
That popped up a simple "Ok" style dialog box with the message "Hello World!". Now I can start thinking of more interesting things to do with JRuby!
1 The Windows Command Prompt is accessible via the Start menu, under Accessories. Or you can select Start -> Run, then enter "cmd".
