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.
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