Ruby has become one of the most popular dynamic languages in recent years. A JavaWorld article shows how to use new JDK features to extend Java applications with Ruby-based scripting, and how to run Ruby code inside the JVM.
While discussions about programming languages often point to the differences between statically and dynamically typed languages, in the context of Java, the static-vs-dynamic debate may soon become a moot point: JDK 1.6 includes several features that allow many scripting languages to be used in conjunction with Java classes and objects.
In a recent JavaWorld article, Ruby for the Java world, Joshua Fox describes how to use Ruby in the context of some of those new JDK features, as well as JRuby, a Java-based Ruby implementation. The latter is also an example of how code written in a language other than Java can still run in the JVM:
The article illustrates the flexible roles Ruby can assume in a Java application:
JRuby can pull data from one system, transform it and insert it into another. When the requirements change, modifying a JRuby script is as easy as changing a configuration file, thereby avoiding the complex compile-and-deploy cycle of Java integration code.
In addition to just running Ruby code inside the JVM, the article points out the ways in which Ruby code can invoke Java classes and objects, and how Java code can interact with Ruby scripts:
In addition to running ordinary Ruby code, you can also use JRuby to construct Java objects, call Java methods, and inherit from Java classes. A Ruby class can even implement Java interfaces—necessary for statically calling Ruby methods from Java... To initialize the libraries used for accessing Java from Ruby, start with require "java". Then specify the Java classes to use with the include_class method, for example, include_class "javax.jms.Session". You can include an entire Java package into a Ruby module with include_package.
... In addition to calling Java from Ruby, you can call Ruby from Java, making your application scriptable. With JRuby's minimal syntactic overhead, you can create an easy-to-use domain-specific language for users to work with.
What do you think of JRuby and the new JDK scripting framework?