JRuby 0.9.8

Earlier this month, the JRuby team (headed up by Charles Nutter) released JRuby 0.9.8. See his post for notes on significant improvements on performance and language compliance. For us it was a seamless upgrade for the embedded JRuby engine in Photon.


We dropped in the new jruby.jar into our custom org.jruby.bsf plugin. There appears to be a new dependency on the ASM library, presumably to better support the ability for Ruby classes to extend Java classes. You can now call methods from Java code that were overridden in Ruby code, the major reason we are interested in the new release. Say you have an abstract Java class called Calculator:

package jrubytest;

public abstract class Calculator {
  public abstract Number calculateSomething();
}

And you want to do the calcuation in Ruby, perhaps so that a user could specify it at runtime. You have the following definition for the MyCalculator subclass of Calculator in Ruby:

require 'java'
include_class "jrubytest.Calculator"

class MyCalculator < Calculator
  def calculateSomething
    9**15
  end
end

MyCalculator.new()

This final code snippet shows how you can call the calculateSomething() method on the instance of MyCalculator created by the JRuby engine. This assumes that the variable bsfEngine has an instance of BSFEngine and the rubyCode variable has the Ruby code given above (get the complete source here).

// Execute the code and get the result
// of "MyCalculator.new()" as a Java object
Object result = bsfEngine.eval("<java>", 1, 1, rubyCode);
if (result instanceof Calculator){
  System.out.println("Calculated: "+
    ((Calculator)result).calculateSomething());
} else {
  System.out.println("Unknown result "+result);
}

This gives the result, “Calculated: 205891132094649”, which as we all know is 9 to the 15.

The JRuby guys are making great progress. Stay tuned for a set of integration tests built on top of Photon and the embedded JRuby engine.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s