Tuesday, October 03, 2006

New JRuby Syntax for Importing Java Classes

We've added a cleaner, simpler syntax to JRuby for including Java classes into a given namespace. It stemmed from a response of mine to a user on the JRuby mailing lists, and has been committed to trunk already. Almost everyone on the list agreed this new syntax is better.

Tom sums up the history of JRuby's Java include syntax in his blog post, but here's the executive summary.

Where before you would use this:

include_class "java.util.ArrayList"
include_class("java.lang.String") { |p,n| "J" + n }

You now can simply do this:

ArrayList = java.util.ArrayList
JString = java.lang.String

And this syntax also works directly; no assignment to a constant is required:

class MyClass < java.awt.event.ActionListener

Pretty slick, if you ask me.


jherber said...

it would be nice to include_class java.sql.*

it would also be of great help have a way to dynamically load a jar onto the classpath. perhaps a ruby friendly way such as require_jar $jarname and require's cousin load_jar.

there should be an easy way to 'namespace' all of java. something like include_class java.lang.* into_namespace 'JAVA' or such.

buriya said...

can u specify the steps of calling a third party Java class in a Ruby scipt.