1. Computing

Why JRuby?

By

Why JRuby?

When you learned Ruby you probably learned on the official Ruby interpreter known as Matz's Ruby Interpreter (or MRI). It's reasonably fast, especially considering the dynamic nature of Ruby code, well maintained and updated, has a vast library of code available, and is generally a very good interpreter for Ruby. But MRI is not the only game in town, there are other interpreters out there. Among them there is JRuby, a Ruby interpreter that runs on the Java Virtual Machine (JVM).

As soon as Java is mentioned a lot of programmers will simply turn their backs. Java is a mixed bag. It has some great strengths (portability, JIT compiling) but some rather unappealing weaknesses (poor startup times, high memory usage). Many programmers simply don't want anything to do with Java, so why should you consider using a Ruby interpreter written in and running on Java?

Performance

JRuby is no slouch when it comes to performance. It is, on average, about as fast as Ruby 1.9. As this benchmark shows it's faster on some benchmarks but slower on others. But the main idea to take away from these benchmarks is that JRuby is not slow. To some programmers, Java and especially other languages running on the JVM are considered to be slow. This just isn't the case.

JRuby also has the capability to be faster than MRI. With Just In Time compiling and Ahead of Time compiling your Ruby code is closer to the metal than ever before. Future versions of JRuby will only improve performance further.

However, you may notice the absolutely enormous memory usage of the JRuby benchmarks. The JVM just uses a lot of memory. But in this day and age with even cheaper computers having 4 gigabytes of memory, this may not be an issue for you.

Java Interaction

Your JRuby code can interact with Java objects as if they were Ruby objects. This means you have access to every Java API and jar you can think of. This is everything from third party jar files to GUI APIs like AWT and Swing. If you're unfamiliar with just how many libraries this is, browse through the Java 6 API documentation. The number of packages on the top left is outright intimidating, and you can use every bit of it from your JRuby code.

Ruby has the gems library of code to draw from. If you need something done (text formatting, for example) there may be a gem for it already. However, these gems have only been very popular for the past 5 or 6 years. Java has been around twice as long and the library of code is even larger. However, there is no single code repository for the jar files like Rubygems.

Applets

A popular use for Java is to embed a Java program into a website and run the program as a Java applet. Since JRuby runs on Java, you can embed your Java programs into a website very simply. Without Java, embedding your Ruby code on a website requires a server-side component or a special browser plugin.

To see this in action, take a look at Try JRuby, an IRB session in your browser accessible from any computer with a web browser and Java. Another advantage is the ability to use Java WebStart files, a way for users to launch programs from a file, but for the code to be delivered via the web.

You Don't Need to Know Java

You don't need to know C in order to use the MRI interpreter (which is written in C). Similarly, you don't need to know Java in order to use JRuby. Every bit of knowledge helps, knowing C and how the MRI works is really going to help you understand how your Ruby code is running on the MRI. But you don't need to know it, and you don't need to know Java in order to use JRuby.

Distribution

Have you ever tried to write a Ruby program and distribute it to others? Your first idea there might be GitHub or the like to share the code with others, but what about distributing your program to people who aren't programmers? To people who have no idea what Ruby is? And to people on multiple platforms?

This is perhaps one of the strongest reasons to use JRuby. Your Ruby code can be packaged up into a Jar file and run by anyone with Java (and most people do have Java) on Windows, OS X, Linux and others. Using MRI you have to use separate packaging software and, in general, there is a lot of finger crossing involved. It's just so much simpler with JRuby.

Why Shouldn't You Use JRuby?

If nothing in the above list appeals to you, there's no real reason you should use JRuby. MRI isn't going anywhere any time soon, it works on all the major platforms and there's just no reason for many Rubyists to stray from it.

There are also some disadvantages to using JRuby. The first very noticeable one is that startup times are very slow. The JVM is big and takes a few seconds to start up. If you run a lot of small scripts from the command line, this is going to be very annoying. What essentially takes 0 second with MRI could take up to 15 seconds because of this startup time.

  1. About.com
  2. Computing
  3. Ruby
  4. Beginning Ruby
  5. Why JRuby?

©2014 About.com. All rights reserved.