tags:

views:

151

answers:

1

Hey,

I have a ruby file as follows:

module Example
    class Myclass
        def t_st
            "Hello World!"
        end
    end
end

now if this was just a class I would be able to use the following java code:

ScriptEngine jruby = new ScriptEngineManager().getEngineByName("jruby");
jruby.eval(new BufferedReader(new FileReader("example.rb")));
Object example = jruby.eval("Myclass.new");

However, this class rests inside a module. Calling the same code as above produces the error:

Exception in thread "main" org.jruby.embed.EvalFailedException: uninitialized constant myclass

In addition, calling:

Object example = jruby.eval("Example");

The module returns no error. So one would assume this follows the format for Ruby.

Object example = jruby.eval("Example::myclass.new");

Again however, I get the same error as before.

Can anyone help? As there is little documentation on JRuby?

Thanks

A: 

Make sure that you do not have syntax errors. Usually I get those errors when I'm not paying attention to what I write...

Secondly, you cannot write the following:

Object example = jruby.eval("Myclass.new");

The reason being that your class is in a module. Instead, use the this:

Object example = jruby.eval("Example::Myclass.new");

Other than that, I don't know what the problem could be. For myself, I was able to run the following code under Java 1.6 and with jruby-engine.jar and jruby-complete-1.4.0.jar under my classpath.

package test;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class MyJavaClass {
    public static void main(String arg[]) throws ScriptException,
            FileNotFoundException {

        ScriptEngine jruby = new ScriptEngineManager().getEngineByName("jruby");
        jruby.eval(new BufferedReader(new FileReader("example.rb")));
        Object example = jruby.eval("Example::Myclass.new");
        jruby.put("a", example);
        System.out.println(jruby.eval("$a.t_st"));

    }
}
Pran
your right was that
James Moore