Question about the java scripting API
I successfully managed to embed Groovy and JRuby as scripting engines in my application, but there is one thing that puzzles me:
With both languages, it is not possible to retrieve the value of a local variable that has been previously assigned.
For example:
jRubyEngine.eval("a=1") // works
then
jRubyEngine.eval("a")// throws an exception
Similar for groovy
groovyEngine.eval("String x = \"asdf\"") //works
groovyEngine.eval("x") //exception
Global variables work just fine though.
So my question is: why are local variables lost between calls of eval()?
Is there a way to preserve them and the whole local context?
And: is there a forum here that is more appropriate for questions about the scripting API?
[792 byte] By [
johann_pa] at [2007-11-27 8:39:46]

> You do know that javascript and java are about as
> closely related as genius is to the intelligence of
> some of the posters here.
>
> find a javascript forum. try javaranch
>
> Message was edited by:
> petes1234
:) JRuby and Groovy have nothing to do with JavaScript.
They are scripting languages which run on the JVM.
> I successfully managed to embed Groovy and JRuby as
> scripting engines in my application, but there is one
> thing that puzzles me:
Where did you get them? Or did you implement this yourself?
> With both languages, it is not possible to retrieve
> the value of a local variable that has been
> previously assigned.
>
> For example:
>jRubyEngine.eval("a=1") // works
> n
>jRubyEngine.eval("a")// throws an exception
> Similar for groovy
>groovyEngine.eval("String x = \"asdf\"") //works
> groovyEngine.eval("x") //exception
>
> Global variables work just fine though.
> So my question is: why are local variables lost
> between calls of eval()?
> Is there a way to preserve them and the whole local
> context?
>
> And: is there a forum here that is more appropriate
> for questions about the scripting API?
What does the following produce?
engine.eval("a=1;");
Object result = engine.eval("a;");
System.out.println(result);