Add a property to scripting container

Hi all,

Is there any way to add some kind of “property” (an arbitrary Java
object) to a ScriptingContainer instance before a script is run. The
idea is the property can then be looked up from within any script run
inside that container.

I don’t want to set the property as a Ruby global variable since I don’t
want to pollute the namespace.

I.e. I want to do something like this:

Java:

ScriptingContainer container = new
ScriptingContainer(LocalContextScope.SINGLETHREAD);
container.putSomeArbitraryObject(“foo”, new Foo());
container.runScriptLet(…)

And in the Ruby script

puts “I’m in the script”

Get a reference to the scriptingContainer

foo = self.getContainer().getArbitraryObject(“foo”)

Basically I need some way of communicating an object between the Java
code which starts the script out Ruby API which is instantiated in the
script.

Right now I am using Java static members to do this, i.e.

Java:

MyClass.staticField = new Foo();

Ruby:

foo = MyClass.staticField

However, this is pretty ugly and it means I can only have communicate to
one script at any one time


Tim F.

Vert.x - effortless polyglot asynchronous application development

twitter:@timfox

Hi Tim,

On Apr 5, 2012, at 2:06 AM, Tim F. wrote:

ScriptingContainer container = new
ScriptingContainer(LocalContextScope.SINGLETHREAD);
container.putSomeArbitraryObject(“foo”, new Foo());
container.runScriptLet(…)

And in the Ruby script

puts “I’m in the script”

Get a reference to the scriptingContainer

foo = self.getContainer().getArbitraryObject(“foo”)

If you only want the arbitrary variable to be accessible at the top
level of the script evaluation, you can do this:

In Java

container.put(“foo”, new Foo());
container.runScriptlet(…)

In script, foo is an available local variable

puts “I’m in the script. Foo is #{foo}.”

Rhett

On 05/04/12 08:17, Rhett S. wrote:

I.e. I want to do something like this:

Get a reference to the scriptingContainer

foo = self.getContainer().getArbitraryObject(“foo”)
If you only want the arbitrary variable to be accessible at the top level of the
script evaluation, you can do this:

In Java

container.put(“foo”, new Foo());
container.runScriptlet(…)

In script, foo is an available local variable

puts “I’m in the script. Foo is #{foo}.”

Sorry I probably wasn’t clear:

I don’t want the object to be accessible as a Ruby variable, local or
otherwise.

Also the object needs to be accessible from other scripts that might
have been loaded by the top level script.