11

jruby 1.7.23 (1.9.3p551) 2015-11-24 f496dd5 on Java HotSpot(TM) 64-Bit Server VM 1.7.0_79-b15 +jit [Windows 7-amd64]

I run a scriptlet in my scripting container like this:

(RubyObject) ro = (RubyObject)container.runScriptlet(org.jruby.embed.PathType.RELATIVE,"example.rb");

where container is of type ScriptingContainer. The "local variable behaviour" is set to "transient".

The file example.rb contains the following lines:

foo='xxx'
$bar='yyy'
'zzz'

I can retrieve from the Java side the value 'zzz', because it is explicitly returned from runScriptlet. I can also retrieve the value of $bar using

container.get("$bar")

Is there a way to retrieve the value of foo? container.get("foo") returns the null pointer.

I also tried to change the local variable behaviour to "persistent", but still same result.

user1934428
  • 19,864
  • 7
  • 42
  • 87

1 Answers1

0

I think you have already used this ScriptingContainer(LocalVariableBehavior.PERSISTENT) but that necessary mean that you can already access most of what is inside of this scriptContainer.

Maybe, we can modify your code a little bit and try using parse. Please see example 4. It may enlighten you. Hope this helps.

bipartite
  • 91
  • 7
  • No, as I wrote, I set the local variable behaviour to TRANSIENT. I don't want to keep local variables, and I don't need to access local variables stored in the scripting container. Maybe my understand of transient vs. persistent is flawed. From my understanding, persistent applies to local variables inside functions. From your response, it sounds that it also applies to variables in the toplevel environment of the script. If this is the case, I understand, why my variable `foo` can't be accessed. – user1934428 Jan 29 '16 at 08:48
  • I see that I have to clarify something (it was not clear to me that this is an important point, but seemingly it is): My script is run only once. The only purpose of this run, is to create several Ruby objects, which are then accessed from my Java code. The Java code stores references to these Ruby objects and uses them in the sequel as if they were plain Java objects. Of course I ensure, that there is always a reference to the ScriptingComtainer be left alive, lest it might get garbage collected, and I'm not sure what effect this would have on my Ruby Objects.... – user1934428 Jan 29 '16 at 08:57