The first two chapters examined JRuby almost entirely from a Ruby-centric perspective. In the next few chapters, we look at leveraging JRuby more as a toolkit for Ruby and Java integration. There are two primary integration approaches that we will explore in this chapter. The first is how JRuby can be used to add functionality to a Java application; the second is how Ruby programs can take advantage of the wide array of preexisting Java libraries. Frequently, these types of integration are combined. For example, when mixing Java and Ruby code, using a consistent configuration for application logging can be useful, something which is explored in Logging from Ruby with Jakarta Commons Logging.
There are three primary APIs for embedding Ruby into a Java application:
The JRuby low-level API
The Bean Scripting Framework (BSF)
Java Scripting, defined by JSR 223
These APIs are the subject of the first three recipes in this chapter. The differences between the low-level API and either BSF or Java Scripting are fairly obvious—the low-level API ties your Java code directly to JRuby, whereas both BSF and Java Scripting are abstractions of the JRuby runtime and, in fact, support multiple scripting languages. In general, you will use the JRuby API when you need tight control over the runtime’s configuration. The choice between BSF and Java Scripting is largely based on deployment environment—BSF support is more consistent on Java 5, whereas Java Scripting is ...