Opentopia Directory Encyclopedia Tools

JRuby

Encyclopedia : J : JR : JRU : JRuby


JRuby is a pure 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.) It is likely to be widely used for scripting applications such as games.

Installation

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 \jre\lib\ext or add it to your CLASSPATH.

Using the JRuby Interpreter from Java

Instantiating the JRuby interpreter

The most direct method of running JRuby in Java is as follows:

import org.jruby.Ruby.*;

IRuby runtime = org.jruby.Ruby.getDefaultInstance();
runtime.evalScript("puts 1 + 2");// Other methods for running scripts exist; see docs

Embedding with Bean Scripting Framework

The Bean Scripting Framework, when used with JRuby, will allow you more conveniently to pass your own Java objects to your JRuby script. You can then use these objects in JRuby, and changes will affect your Java program directly. To run a JRuby script using BSF, you must first copy the BSF.jar file into your /lib/ext/ folder. Then, try the following:
import org.jruby.Ruby.*;
import org.jruby.*;
import org.jruby.javasupport.bsf.*;
import org.apache.bsf.BSFException;
import org.apache.bsf.BSFManager;

JLabel mylabel = new JLabel();
BSFManager.registerScriptingEngine("ruby", "org.jruby.javasupport.bsf.JRubyEngine", new String[] );
BSFManager manager = new BSFManager();

/* Import an object using declareBean then you can access it in JRuby with $ */

manager.declareBean("label", mylabel, JFrame.class);
manager.exec("ruby", "(java)", 1, 1, "$label.setText(\"This is a test.\")");

JRuby Programming

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. For more information read Ruby programming language.

Calling Java from JRuby

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:
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)

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

See also

External links

 


From Wikipedia, the Free Encyclopedia. Original article here. Support Wikipedia by contributing or donating.
All text is available under the terms of the GNU Free Documentation License See Wikipedia Copyrights for details.

Search Titles
0123456789
ABCDEFGHIJ
KLMNOPQRST
UVWXYZ?

E-mail this article to:

Personal Message: