SEARCH
0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Prev | Current Page 188 | Next

Ola Bini

"Practical JRuby on Rails Web 2.0 Projects: Bringing Ruby on Rails to Java"

Of course, you can do many other things too.
What I??™ll show you here is three different ways to call out to custom Ruby code from Java.
I??™ll show a complete Java program for each approach, but they are about as minimal as they
can get. The approaches are ordered by level of recommendation. That is, you should probably
use JSR223 if you can; if not, use Bean Scripting Framework (BSF), and if that also fails, use
the JRuby runtime directly.
The JRuby Runtime
Let me begin this section by saying this once again: you probably shouldn??™t call out to the
JRuby runtime directly. There is almost certainly a better solution. The reason it??™s better to use
one of the standardized frameworks is that the runtime demands that you wrap and unwrap
CHAPTER 6 ?–  JAVA INTEGRATION 111
values manually. There are many more things to keep in mind, and you can??™t be certain that it
will just work, as BSF and JSR223 pretty much guarantee.
So, on to the example program. You want to get some data in there too, so the example
program will print out the result from adding 13 to a value you send in from Java. Here??™s the
code to achieve this (and this is pretty much the most minimal Ruby runtime code there can
be). It will be more complex for more complicated tasks.
package c6;
import org.jruby.Ruby;
import org.jruby.javasupport.JavaEmbedUtils;
public class JRubyRuntime {
public static void main(String[] args) {
Ruby runtime = Ruby.getDefaultInstance();
runtime.


Pages:
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200