Wonderful Introduction to JRuby JRuby - Wikipedia, the free encyclopedia JRuby is a Java implementation of the Ruby interpreter, being developed by the JRuby team.
JRuby is free software released under a three-way CPL/GPL/LGPL license.
JRuby is tightly integrated with Java to allow the embedding of the interpreter into any Java application with full two-way access between the Java and the Ruby code. (Compare Jython for the Python language.)
JRuby is Ruby on the JVM. It is a Ruby interpreter written entirely in Java. JRuby allows using Java capabilities with Ruby syntax. It also allows using Ruby libraries from Java.
Many programming languages have been implemented on the JVM. The list includes BeanShell, Bex (BeanShell variant), Groovy, Jaskell (Haskell), Jawk (AWK), JudoScript, Jython (Python), JRuby (Ruby), Pnuts, Quercus (PHP), Rhino (JavaScript), SISC (Scheme), Sleep (Perl/Objective-C), Jacl (TCL) and more. A full list is available at http://scripting.dev.java.net/.
JRuby supports all Ruby syntax and built-in libraries. It supports most of the Ruby standard libraries. Those implemented in Ruby were easy to retain. Those implemented in C had to be ported to Java. Only the most frequently used of those libraries have been ported so far. Many popular Ruby libraries work under JRuby today. These include Active Record (with JDBC), DRb, Rake (Ruby's answer to Make and Java's Ant), Rails, RSpec (behavior-driven development) and RubyGems.
JRuby History
Stephen Matthias Aust ported the Ruby grammar from the C-based Ruby interpreter to Jay, a Java-based parser which is still used by JRuby. Jan Arne Petersen started JRuby project in 2001, building on the work by Aust. Thomas Enebo began work on JRuby in late 2002 and became the project lead in late 2003. Charles Nutter began work on JRuby in 2004. Sun Microsystems hired Nutter and Enebo to develop JRuby full-time in September, 2006. This was at least partially motivated by Tim Bray at Sun who is a major advocate of dynamic languages. Sun is committed to keeping JRuby open source and will provide more Ruby development tools such as support in NetBeans. Ola Bini became a committer in October, 2006. Nick Sieger became a committer in January, 2007.
What JRuby Offers That Ruby Doesn't
JRuby classes can inherit from Java classes and implement Java interfaces. They can even add methods to existing Java classes. These methods are visible from JRuby, but not Java. We'll discuss this in more detail later. (Java classes can't inherit from a JRuby class.)
Running on the JVM provides many benefits. As mentioned earlier, this allows Java libraries to be used from Ruby syntax. It also provides features Java enjoys such as use of native threads, Unicode support (surprisingly weak in Ruby), and portability to any platform with a JVM (which is most platforms).
Installing JRuby
To install JRuby, download a binary release from http://www.jruby.org. Unzip/untar the downloaded archive, set the JRUBY_HOME environment variable to point to resulting directory, and add $JRUBY_HOME/bin to the PATH environment variable.
To checkout the latest version from the trunk of the Subversion respository, run the following command.
svn co http://svn.codehaus.org/jruby/trunk/jruby
To build this, ensure that Java and Ant are installed, and run "ant".
Java Counterparts in JRuby
To use java in ruby, first of all:
include Java (require 'java' doesn't work after 1.0)
Accessing External Classes:
require "/path/to/xxx.yyy.jar" and
include_class Java::xxx.yyy.MyClass
Implementing a Java Interface
class SomeClass
include RunCompare
...
end
Import a java package:
import => include_class :include_class java.lang.System