javasupport.bsf.JRubyEngine",
new String[] { "rb" });
BSFManager manager = new BSFManager();
manager.declareBean("label", "hello world", String.class);
manager.exec("ruby", "(java)", 1, 1, "puts $label");
}
}
First of all, you only need to import the BSFManager. This is the central point for all evaluations
with BSF, and should be the only thing needed. You first need to register Ruby as a script
engine, so that BSF can recognize which evaluator to use. The String array passed to registerScriptEngine
details which file endings this script engine should evaluate. After you??™ve
registered Ruby, you create a new manager, and then declare a bean on that manager. As you
can see, there??™s no mention of wrapping or converting the object. BSF does this completely
beneath the covers. The exec method takes several parameters that you can use for detailed
error reporting. The first string is which language you??™re evaluating. The next is the file name
this code belongs to. The numbers are line and column indexes where this code comes from.
To compile and run this code, place it in c6/JRubyBSF.java and execute these commands:
javac c6/JRubyBSF.java
javac -cp ${CLASSPATH}:. c6.JRubyBSF
If everything works as expected, you should see hello world printed on your console.
Of course, this approach is more verbose than using the JRuby engine directly. That is
mainly caused by the initial setup and the need to provide some extra information to the evaluation
process.
Pages:
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203