Description. Installs JRuby into /usr/local/lib/jruby. Binaries are linked to /usr/local /bin/jruby. /usr/local/bin/ is added to $PATH. Description. Installs JRuby into /usr/local/lib/jruby. Binaries are linked to /usr/local /bin/jruby. Requirements. Platform: Debian, Ubuntu (tested on. Introduction This final chapter includes a series of recipes about how to participate in the JRuby community. First, we will look at building JRuby from source.
|Published (Last):||18 June 2008|
|PDF File Size:||13.94 Mb|
|ePub File Size:||13.94 Mb|
|Price:||Free* [*Free Regsitration Required]|
It allows Ruby applications to be run within a Java Virtual Machine and interface with libraries written in either Java or Ruby. Although the Cooibook project was initiated ininterest in JRuby has grown significantly over the last few years, reflecting an overall growth in interest in Ruby sparked by the success of the Ruby on Rails framework. The website for the JRuby project is currently http: Ruby is a dynamic object-oriented programming language created by Yukihiro Matsumoto, known by the cookbookk Matz, in the coookbook.
Ruby follows a style of versioning similar to the Linux jrhby, where an even minor version number indicates a stable release and an odd minor version number indicates a development release. As a result, there are two current versions of Ruby: The standard Ruby interpreter [ 1 ] is written in C. NET frameworkand Rubinius. Ruby does not have a formal language specification; however, one is being developed through the wiki at http: As an object-orientated language, many of the underlying concepts of Ruby will be familiar to Java developers, even if the syntax jrjby not.
In Ruby, a block is a grouping of code that gets passed jrubj a method call. The receiving method can invoke the block any number coolbook times and can pass parameters to the block. Support for a similar type of element, a closureis being contemplated for inclusion in Java 7; there are several competing proposals and it is unclear which proposal, if any, will be adopted.
The former syntax, using braces, is typically used to create a block for a single statement. The latter syntax, using the do and end keywords, is typically used for multistatement blocks. The Ruby yield function transfers control to the block argument. Ruby has an active developer community both online and in local developer groups. The Ruby language website, http: A wide array of books about Ruby have been published, perhaps most famously Programming Ruby: JRuby began its life as a direct port of the C-based interpreter for Ruby 1.
For the next few years, it was an interesting project, but had serious performance limitations. Following the release of Ruby 1. Since then, a third lead developer, Nick Sieger, has become a Sun employee. For users, JRuby represents a different opportunity: This area will be explored in the first two chapters.
With the release of JRuby 1. In terms jduby compatibility, the JRuby project strives to duplicate the behavior of the standard Ruby interpreter whenever possible, even at the expense of consistency with Java.
Most of the core Ruby classes are included, as is much of the standard Ruby library, the RubyGems package management system, RDoc documentation support, and the Rake build system.
Despite these efforts at compatibility, there are some areas where JRuby deviates from behavior exhibited by the C Ruby interpreter. The most visible example of this is how JRuby handles threads.
In this case, however, JRuby is actually ahead of the standard Ruby interpreter in that Ruby 2. Download and extract jtuby latest binary release from the JRuby website, http: Add the bin directory to the PATH environment variable.
Commercial and open source software packages are available that include support for TGZ files, such as WinZip coobkook It is not jfuby to install JRuby in any particular location on your computer. My preference is to install Java libraries and executables in subdirectories of C: The results of extracting the binary for the latest release at the time of this writing, 1.
After extraction, JRuby is ready to be used. Like irbjirb allows you to iruby Ruby statements and immediately see the results of each statement.
JRuby includes both command-line and GUI versions of jirb in the bin directory. In both figures, jrhby trivial Ruby code has been executed. You can see that kruby the output of the puts method Hello World and its result nil have been output.
If you launch either jirb. You can also test JRuby from the command line by using the -e evaluate option:. On the Advanced tab, click the Environment Variables button. You could also simply prepend the full path to the bin directory to PATHbut using a separate environment variable makes upgrading a bit easier.
Once you have configured the environment variables, click OK. These changes will only be reflected in newly opened windows something to keep in mind if you have any command-line windows open. After adding the bin directory to your PATHyou can then simply run the test shown previously by executing:. Although most Linux distributions and OS X include utilities for extracting both types of files, TGZ files are preferable because files extracted from them include permission settings, something that is not the case with ZIP files.
The JPackage Project at http: If you have root privileges on the system where you want JRuby installed, you should install JRuby based on whatever standards already exist. This will facilitate upgrades later:. Once the bin directory has been added to your PATHyou can test the install by running a simple Ruby script:. Building JRuby from Source. Using Both Ruby and JRuby. Use the RubyGems support built into JRuby. To install a package, run:.
For example, to install the Ruby on Rails web framework, use: RubyGems is the standard package management and distribution system for Ruby packages. There are thousands of packages, referred to as gemsavailable through the default RubyGems repository at http: Although some gems are specific to the C Ruby implementation or JRuby, most are compatible with any Ruby implementation.
Common RubyGems commands include install cookbkok, queryupdateuninstalland rdoc. The full list can cookobok output by using the help command: The RubyGems Manuals, http: You have Ruby and JRuby installed on the same computer and want to ensure that a Ruby script is processed by the correct interpreter.
Use the -S command-line argument for the ruby and jruby executables. For example, RubyGems is traditionally invoked with a command like: Popular Ruby packages such as Rake, Ruby on Rails, and RubyGems include their own executable Ruby scripts that most guides, both online and print, iruby you to invoke directly. Because there are so many variables, this recipe prescribes cokbook a single, consistent method, passing the script name through the -S command-line argument to either the ruby or jruby executables.
This advice is particularly significant in the context of the RubyGems script, gem.
To create a new Rails application, you could run either:. You can see this by passing environment to the gem script:. Managing Packages with RubyGems. You already have a number of RubyGems installed and want to use those gems from JRuby without reinstalling the gems.
This value can be seen in the output of gem environmentwhere it is referred to as the installation directory: Whereas some RubyGems are implemented entirely in Ruby, many are implemented in a combination of Ruby and C or, in a growing number of cases, Ruby and Java.
However, those implemented in a mixture can only be installed using a compatible interpreter. The list of supported platforms for each interpreter can be seen in the output of gem environment.
JRuby Cookbook – O’Reilly Media
Because the RubyGems runtime knows this list of supported platforms, it is possible to mix gems supporting different platforms in cookboook same directory; the runtime will select the appropriate libraries.
You want to write Ruby code that uses one or more Java classes. First, you need to tell JRuby that you will be referencing Java classes from your Ruby code. Do this by including an include declaration at the top of your Ruby file: The syntax for referencing a specific Java class depends on the package in which the jjruby resides.
To do this, pass a block to the function. JRuby makes referencing Java classes relatively natural from the perspective of a Java developer.
For the most commonly used packages, you can use import just as you would in Java code. When calling methods on a Java class, JRuby handles some type conversion for you—instances of basic Ruby classes such as FixNumFloatand String cookboko converted to instances of the corresponding Java classes when passed to Java objects.
JRuby includes implementations of the java. Map interfaces for handling Ruby Array and Hash objects. JRuby provides access to public static methods and variables through the:: You need to pass a Ruby array to a method that accepts a Java array of a specific type. For example, creating an array of javax. StreamSource objects would be done like this:. Primitives, as well as java. Nrubyhave Ruby symbols assigned to them. For example, to create an array of int primitives:.
For example, calling a method through Java Management Extensions JMX involves passing two arrays to the invoke method of javax. MBeanServerone of Object instances, storing the method parameters, and one of String instances, storing the method signature.