On Thu, Jun 18, 2009 at 6:07 PM, brightmatter <[email protected]>wrote:
>
> Calling a fully developed Ruby class from Java,
>
> Hello, and thank you for taking my post. I am a short-time reader,
> first-time poster to this forum. I am having trouble with JRuby. I don't
> understand even the most simple actions. Let me explain. I have a Ruby
> file named "test_method.rb". Inside is a class consisting of this code.
>
>
> module Test_Module
> class Test_Class1
> def test_function1()
> puts('Hello')
> end
> end
> end
>
>
> I have a Java program I am running and I have this code. I found bits and
> peices around the net and I assymbled this fankencode.
>
>
> import java.io.BufferedReader;
> import java.io.File;
> import java.io.FileReader;
> import javax.script.*;
>
> public class Stream_Listeners {
> public Stream_Listeners() throws Exception {
> ScriptEngineManager factory = new ScriptEngineManager();
> ScriptEngine engine = factory.getEngineByName("jruby");
> File file = new
> File("C:\\NetBeansProjects\\JavaSwingGUITest\\src\\test_method.rb");
> BufferedReader br = new BufferedReader(new FileReader(file));
> Object obj;
> ScriptContext context = engine.getContext();
>
> //Don't understand this next line...
> context.setAttribute("label", new Float(1), ScriptContext.ENGINE_SCOPE);
> try {
> engine.eval(br);
>
> //Don't understand this next line...
> obj = engine.getContext().getAttribute("begin");
> } catch (ScriptException exception) {
> exception.printStackTrace();
> }
> }
> }
>
>
> This code both compiles and runs without error except for one thing. How
> do
> I access the method "test_function1()" held in the ScriptEngine "engine"?
> It is important to note that I intend to have multiple methods in each
> class
> and multiple classes in each module all in a single file. My question is
> to
> get me started with the very most basic problem of accessing the Ruby
> methods.
You can invoke functions defined in Ruby classes by using
javax.script.Invocable#invokeMethod() method. But, your programs are not
enough to do that. First, you need to instansite an object in Ruby as in
below:
module Test_Module
class Test_Class1
def test_function1()
puts('Hello')
end
end
end
Test_Module::Test_Class1.new
When JRuby engine evaluates this Ruby script, engine gets the instance of
Test_Class1, which has the function, "test_function1(). Then, JRuby engine
can invoke test_function1() method through invokeMethd() like this:
Object object = engine.eval(br);
Invocable invocable = (Invocable) engine;
invocable.invokeMethod(object, "test_function1");
Documents listed
http://kenai.com/projects/jruby/pages/WalkthroughsAndTutorials#JSR_223_scriptingwould
help you to understand how to use JSR223 scripting API.
-Yoko
>
> I think it should be noted that I have JRuby installed and working. I am
> able to get this to work when I stripe module and class information out of
> the file and use Invocable() on the ScriptEngine. Somehow I need to make
> the leap to using fully made Ruby classes.
> --
> View this message in context:
> http://www.nabble.com/Calling-a-fully-developed-Ruby-class-from-Java-tp24099675p24099675.html
> Sent from the JRuby - Dev mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
> http://xircles.codehaus.org/manage_email
>
>
>