JRuby
From Wikipedia, the free encyclopedia
| JRuby | |
| Developer: | Charles Nutter and Thomas Enebo |
|---|---|
| Latest release: | 0.9.8 / March 5, 2007 |
| Platform: | Java Virtual Machine |
| Use: | Ruby programming language interpreter |
| License: | CPL/GPL/LGPL |
| Website: | http://jruby.codehaus.org/ |
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's lead developers are Charles Nutter [1] and Thomas Enebo [2]. In September 2006, Sun Microsystems hired them to work on JRuby full time.[1]
Contents |
JRuby was originally created by Jan Arne Petersen, in 2001. At the time and for several years following, the code was a direct port of the Ruby 1.6 C code. With the release of Ruby 1.8, an effort began to update JRuby to 1.8 features and semantics. Since 2001, several contributors have assisted the project, leading to the current (2007) core team of four members.
The upcoming Netbeans Ruby Pack, will allow IDE development with Ruby and JRuby, as well as Rails for the two implementations of Ruby [3] [4]. A NetBeans Ruby Pack preview is available in the recent Milestone 7 release of NetBeans 6.
JRuby supports Rails since version 0.9 (May 2006) [5], with the ability to execute RubyGems and WEBrick. Since the hiring of the two lead developers by Sun, Rails support (in the areas of compatibility with the reference implementation and speed) has improved a lot. Version 0.9.8 of JRuby executes correctly 98% of Rails own specific test cases [6] [7].
Since early 2006, the current JRuby core team has endeavored to move JRuby beyond being a simple C port, to support better performance and to aid eventual compilation to Java bytecode. To support this end, the team set an ambitious goal: to be able to run Ruby on Rails unmodified using JRuby. In the process of achieving this goal, the JRuby test suite expanded to such extent that the team gained confidence in the "correctness" of JRuby. As a result, toward the end of 2006 and in the beginning of 2007, they began to commit much more complicated redesigns and refactorings of JRuby's core subsystems.
JRuby is designed to work as a mixed-mode virtual machine for Ruby, where code can be either interpreted directly, just-in-time compiled at runtime to Java bytecode, or ahead-of-time compiled to Java bytecode before execution. At present (Jan 2007) only the interpreted mode supports all Ruby's constructs, but a partial AOT/JIT compiler is available and improving. The compiler design allows for interpreted and compiled code to run side-by-side, as well as decompilation to reoptimize and outputting generated bytecode as Java class files.
Installation of JRuby as a standalone interpreter is easy. Download the binary package from JRuby's SourceForge page. Unzip it, and run the jruby batch.shell file from the bin directory, passing the name of a Ruby (*.rb) file as a command line argument. You may need to set environment variables such as JAVA_HOME and JRUBY_HOME, but if so, the batch/script file will give appropriate error messages.
To embed JRuby as a scripting engine in your Java application, First, decompress the download file. Then copy jruby.jar into the directory
JRuby's language syntax is identical to Ruby's. JRuby is reflective, object-oriented, with brief, more human-readable code. In JRuby, every bit of data is an object, even those that are commonly primitives in other languages, such as the integer.
More JRuby documentation can be found at The JRuby Wiki.
JRuby is essentially the Ruby interpreter, except this version is written entirely in Java. JRuby features some of the same concepts, including object-oriented programming, and duck-typing as Ruby. The key difference is that JRuby is tightly integrated with Java, and can be called directly from Java programs.
One powerful feature of JRuby is its ability to invoke the classes of the Java Platform. To do this, one must first load JRuby's Java support, by calling "require 'java'". The following example creates a Java JFrame with a JLabel using "include_class":
require 'java'
include_class "javax.swing.JFrame"
include_class "javax.swing.JLabel"
frame = JFrame.new()
frame.getContentPane().add(JLabel.new("This is an example."))
frame.pack()
frame.setVisible(true)
As of JRuby 0.9.1 it is also possible forgo "include_class":
require 'java'
frame = javax.swing.JFrame.new()
frame.getContentPane().add(javax.swing.JLabel.new("This is an example."))
frame.pack()
frame.set_visible(true)
JRuby also allows you to call Java code using the more Ruby-like underscore_method_naming and to refer to JavaBean properties as attributes:
frame.content_pane.add(label) frame.visible = true
JRuby can just as easily be called from Java, using either the JSR 223[2] Scripting for Java 6 or the Apache Bean Scripting framework. More information on this is available in the JRuby Wiki.
- ^ Jacki (2006-09-07). Sun Welcomes JRuby Developers. On the Record. Retrieved on 2006-09-09.
- ^ JSR 223: Scripting for the Java Platform Specification Request
- The JRuby home page
- The JRuby Wiki
- JRuby Roadmap for 2007
- JRuby-team interview at JavaPolis 2006
- Bringing Ruby and Rails to the JVM
- Roumen's Ruby Flash Demo (Part One): JRuby on Rails in NetBeans
- Roumen's Ruby Flash Demo (Part Two): Advanced JRuby editing features in NetBeans
- Article on JRuby at IBM DeveloperWorks
- Joshua Fox, "JRuby for the Java World", JavaWorld
- Java Posse, Interview with Charles Oliver Nutter and Thomas Enebo about JRuby