Summary
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.
Advertisement
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 JVM is often contrasted with .Net's multilanguage Common Language Runtime as supporting only a single language. But in fact, the JVM executes not only Java, but also Python, JavaScript, Groovy, Scheme, and many other languages, which means that where necessary, Ruby code can interact with these languages as well. [Editor's note: code written in such languages must still compile to Java bytecode.]
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?
Actually I feel the opposite Javascript is the worse unreadable mistake ever (although very powerfull), that's why it's becoming to be generated by more & more frameworks/tools. Tcl actually suffers from it's unreadibility and as a user of both Python & Ruby I feel Ruby more readable.