Bean Scripting Framework (BSF) Introduction
To include BSF into your Java code you first need to register the JRuby engine:
BSFManager.registerScriptingEngine("ruby",
"org.jruby.javasupport.bsf.JRubyEngine",
new String[] { "rb" });
Now you have to create a new BSFManager:
BSFManager manager = new BSFManager();
After you have some Java objects you can declare (export) them as "beans" in BSF:
manager.declareBean("frame", aFrame, JFrame.class);
These beans can be accessed from Ruby as global variables (in this case `$frame').
You can also register objects:
manager.registerBean("frame", aFrame);
this registered object can be accessed by calling:
$bsf.lookupBean("frame")
method in Ruby. Now you can execute Ruby scripts:
manager.exec("ruby", "(java)", 1, 1, "$frame.setTitle(\"A Frame\")");
Or evaluate Ruby expressions:
Object obj = manager.eval("ruby", "(java)", 1, 1, "(1..10).collect { |e| e ** 2 }.first");
int result = ((Integer)obj).intValue();
Example
There is a simple example BSF in the JRuby source distribution (src/org/jruby/javasupport/bsf/BSFExample.java). Which is also avaibale at:
(C) 2002 Jan Arne Petersen
(C) 2005 Thomas E Enebo