Binding JRuby JSR223接口忽略binidngs
JRuby(1.6.7.2)框架的JSR223 javax.script接口似乎忽略了ScriptContext绑定中的绑定Java值。 我会犯错吗? 下面是一个不起作用的简单示例:Binding JRuby JSR223接口忽略binidngs,binding,jruby,ignore,jsr223,Binding,Jruby,Ignore,Jsr223,JRuby(1.6.7.2)框架的JSR223 javax.script接口似乎忽略了ScriptContext绑定中的绑定Java值。 我会犯错吗? 下面是一个不起作用的简单示例: private void run() throws ScriptException { ScriptEngine engine = new JRubyEngineFactory().getScriptEngine(); LittleClass l = new LittleClass(); e
private void run() throws ScriptException {
ScriptEngine engine = new JRubyEngineFactory().getScriptEngine();
LittleClass l = new LittleClass();
engine.put("l", l);
engine.eval("l.x;");
}
public class LittleClass {
public int x;
public void add() {
x = x + 1;
}
}
或者这是一个已知的问题?默认情况下,局部变量不会在多个计算中存在。见: 要更改此行为,请设置
org.jruby.embed.localvariable.behavior
属性:
System.setProperty("org.jruby.embed.localvariable.behavior", "persistent");
ScriptEngine engine = new JRubyEngineFactory().getScriptEngine();
LittleClass l = new LittleClass();
engine.put("l", l);
engine.eval("l.add");
System.out.println(engine.eval("l.x"));
我得到以下错误:NameError:未定义的局部变量或main:Object(root)的方法“l”:1