由于某种原因,jruby 看不到在 Java 中设置的变量绑定。根据此处的文档https://github.com/jruby/jruby/wiki/Embedding-with-JSR-223,以下示例应该可以工作:
public static void main(String[] args) throws ScriptException {
ScriptEngine engine = new ScriptEngineManager().getEngineByName("jruby");
Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
bindings.put("owner", "Otto");
engine.eval("puts \"hello #{owner}\"", bindings);
}
在我的测试中,我得到了异常:
NameError: undefined local variable or method `owner' for main:Object
<top> at <script>:1
Exception in thread "main" javax.script.ScriptException: org.jruby.embed.EvalFailedException: (NameError) undefined local variable or method `owner' for main:Object
我错过了什么吗?