JRuby is a 100% pure-Java implementation of the Ruby programming language.
I have already written about the beauty and power of Ruby and how all smart people should start using it as soon as possible…
In essence, it allows programmers to write programs in Ruby which run on the Java Virtual Machine. The Ruby programming language is better (IMHO) than the Java programming language and, consequently, one can be more productive using Ruby than Java.
For example, this is a small Java program that reads words from the standard input and output the signature of the word (i.e. the letters of the word in sorted order) followed by the word. Cultured readers will recognize an exercise from Programming Pearls by Jon Bentley:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Collections;
import java.util.Vector;
public class Sign
{
public static void main(String[] args)
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
while (true) {
String word = null;
try {
word = in.readLine();
}
catch (IOException e) {
System.exit(1);
}
if (word==null) {
break;
}
Vector<Character> letters = new Vector<Character>();
for (int i=0; i<word.length(); i++) {
letters.add(word.charAt(i));
}
Collections.sort(letters);
for (Character letter : letters) {
System.out.print(letter);
}
System.out.println(" " + word);
}
System.exit(0);
}
}
46 lines! Not too bad for Java incidentally. Notice that I’ve used generics that have been introduced in Java recently. The heart of the program reads a word in a string, sends its characters to a vector, sort that vector and, finally, output the letters in sorted order.
The same program in Ruby is:
while word = gets
word.chomp!
letters = word.split("")
letters.sort!
puts letters.join("") + " " + word
end
which is an order of magnitude shorter and clearer (chomp! is used to strip the newline from the end of the word when input).
This program is a normal Ruby program and, hence, using Jruby it can be run atop a JVM. I’ve done some tests on my MacBook on a list of 234,936 English words:
Interpreter | Runtime |
ruby 1.8.6 (2007-03-13 patchlevel 0) [i686-darwin] | 5.702s |
ruby 1.8.5 (2007-06-07 rev 3841) [i386-jruby1.0] | 26.347s |
java version “1.5.0_07” | 19.978s |
From this, I conclude that for small programs, plain Ruby is quicker than anything that run on the JVM (another conclusion is that JRuby’s performance is comparable with Java). This is understandable because the Ruby interpreter is designed for small programs like this. But what about when you have to run a substancial Ruby on Rails application?
According to some important people, this is where JRuby really shines…
JRuby allows Ruby on Rails applications to run on a industrial-grade J2EE application server so much so that people have started predicting the demise of pure Ruby application servers like Mongrel. With the emergence of GlassFish, a free, open source, production-quality, enterprise J2EE application server, it becomes quite reasonable to use the following architecture to deploy easy-to-write yet enterprise-grade web applications:
Exciting times indeed :-)
18 September: an update
Eddy has a much quicker version of my Java program that runs in 4.222s and is therefore quicker than the plain Ruby version and is around 6 times quicker than the JRuby version. Eddy uses toCharArray() instead of sending the individual characters of the String in a Vector.
Thanks!
Post Scriptum
If you read the comments, you’ll see that a number of people are missing the point of this post. Maybe it’s badly written. The point is that Ruby of Rails applications can now run on JRuby which can itself run on GlassFish which is a good thing in my opinion.