从java调用ruby模块的方法

时间:2017-09-20 19:23:15

标签: ruby jruby

在test.rb中有一个简单的ruby模块代码:

module Test
def self.update(a,b)
  "Hello world"+a+b
end
end

然后我写了这样的java代码:

public static void main(String[] args) throws FileNotFoundException, ScriptException, NoSuchMethodException {

System.setProperty("org.jruby.embed.localvariable.behavior", "transient");
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("jruby");
Reader reader = new FileReader("/temp/test.rb");
Object receiver = engine.eval(reader);
System.out.println((engine.eval("Test::update")));
}

此代码按预期打印“Hello World”,但我尝试了各种方法将参数传递给方法,如:

SimpleBindings bindings = new SimpleBindings();
bindings.put("a", 1);
bindings.put("b", 2);
System.out.println((engine.eval("Test::update", bindings)));

我无法传递参数,我们如何从java传递参数到模块的方法?

更新:以下方法有效:

System.out.println((engine.eval("Test::update(a,b)", bindings)));

0 个答案:

没有答案