JRuby's killer feature

by Curt Hibbs

JRuby is a Ruby language interpreter that is written in Java. This means that it runs Ruby programs under the JVM and, therefore, runs portably anywhere that Java runs (which is nearly everywhere).

I used to think that the killer feature of JRuby was making it easier to sneak Ruby into companies via a Java back-door. But that was before I saw Charles Oliver Nutter's JRuby presentation earlier this week at the St. Louis Gateway JUG. JRuby's killer feature is its brilliant integration with Java code. Ruby code can call Java code (and vice versa) and Ruby classes can inherit from Java classes.

A really nice touch is that JRuby will let you call Java methods using standard Ruby conventions. This is especially nice for getters and setters, where you can take Java code that looks like this:

MyThing thing = new MyThing("blob");
thing.setName(thing.getName() + "_title");

And write it in Ruby style like this:

thing = MyThing.new("blob");
thing.name += "_title";

This also means that your Ruby code has direct access to that vast universe of Java code. Peter Cooper just wrote about his experiment using JRuby to create a cross-platform desktop GUI application using the same SWT framework that renders Eclipse. Cool stuff!


Terry Laurenzo
2006-11-09 14:12:59
My favorite is the ability to extend the Java proxy classes with Ruby specific behaviors. For example:

# Extend a proxied class implementing the given java interface name
# by including the given Ruby module
def self.extend_proxy_with(java_interface, mod)
JavaUtilities.extend_proxy java_interface do |c|
c.include mod

Calling this method like so:
extend_proxy_with 'org.omg.uml.foundation.core.Classifier', ClassifierExtensionsModule
will mix the ClassifierExtensionsModule into any class that implements the UML Classifier interface. Of course, these extensions only work within the Ruby world, but they sure make life easier when it come to adapting some rigid Java class hierarchy for some other purpose. The program where I used this pattern was an MDA tool that works similar to Andromda but can do without the metafacade decorator layer by just extending the Java classes directly.

So not only does JRuby make it easy to use Java classes. It makes it easy to extend them at runtime as well.

2006-11-12 02:38:29
thing.name += "_title"
Curt Hibbs
2006-11-12 22:44:52
Duh! Thanks Terry, I updated the code with your change.
2007-05-11 07:12:14
Sneaking Ruby in via the JVM is certainly good, but you are right that the real benefit of JRuby is it's access to all that Java code. There's tons of "legacy" stuff out there that you can access from JRuby; code that's too gnarly to re-write. Now it's easy to hook your JRuby front-end into all that stuff. Bonus: it all gets packaged into your .jar/.war/.ear and deploys on the same old app server.

2007-07-27 20:54:51
Thanks so much. Your comments help me understand what is JRuby.
Ed Brannin
2008-01-10 08:24:01
@Terry: I love that feature too, but the code to do it seems to have changed in the past year. With JRuby 1.1RC1, Calling include on the block's parameter fails, but it works fine without the parameter:

def self.extend_proxy_with(java_interface, mod)
JavaUtilities.extend_proxy java_interface do
include mod

(Thanks to The jRuby Cookbook)

2008-05-07 23:02:05
But you are say, that this idead is bad?,
2008-06-10 16:29:01
Of course, but what do you think about that?,
2008-06-11 18:55:10
I want to show you best movies:,
2008-06-12 18:59:55
Try to look here and may be you find what do you want:,
2008-06-14 18:40:26
Most Interested facts about that you can read here:,
2008-06-14 18:40:55
Most Interested facts about that you can read here:,
2008-06-15 16:53:50
I want to show you best movies:,