Setting arguments to embedded jruby script

hi forum members,

I try to call a ruby script twice from a java program, with different
arguments. It seems I cannot set the argv twice; only the first call to
setArgv actually has effect.
There must be someting I have misunderstood. I hope you can help me out
of this.

The following minimal script shows my problem. It outputs

test.rb

string1
test.rb

string1

The jruby script (test.rb) is

argument = (ARGV.length > 0 ? ARGV[0] : ‘default’)
puts ‘test.rb’
puts( " >> #{argument}")

and the java code (Tester.java) is

import org.jruby.embed.ScriptingContainer;
import org.jruby.embed.PathType;

public class Tester
{
Tester (String jrubyhome, String filename, String [] argv)
{
ScriptingContainer container = new ScriptingContainer();
container.setArgv (argv);
container.runScriptlet(PathType.ABSOLUTE, jrubyhome + ‘/’ +
filename);
}

public static void main(String[] args)
{
    new Tester ("/export/home/rudd/jruby", "test.rb",
                new String[] { "string1", "string2" });
    new Tester ("/export/home/rudd/jruby", "test.rb",
                new String[] { "blop", "blop2" });
}

}

I have run this using the jruby 1.5.1.1.jar and the latest
jruby-complete-1.7.3.jar.

As you can see, the second call to ScriptingContainer.setArgv does
nothing. Who can explain to me what I can do be able to run the
script with different argument vectors?

thanks in advance, Rudd

Setting ARGV can best (only?) be done by using ScriptingContainer.put
(“ARGV”, String [] {“a”, “b”})

SetArgv seems broken to me.
On multiple calls of setArgv, getArgv shows the ‘changed’ ARGV, however
only the first call to setArgv has actually effect on the ARGV that is
visible to the scripts.

This is showed by this test script:

============================================================
import org.jruby.embed.ScriptingContainer;

public class Tester
{
Tester (String [] argv)
{
ScriptingContainer container = new ScriptingContainer();
container.setArgv (argv);
String script = “argument = (ARGV.length > 0 ? ARGV[0] :
‘default’)\n” +
“puts( " >> #{argument}”) ";
container.runScriptlet (script);
String [] av = container.getArgv ();
for (int i = 0 ; i < av.length; i++)
System.out.println (“ARGV [” + i + "] is " + av [i]);
}

public static void main(String[] args)
{
    new Tester ( new String[] { "blep"});
    new Tester ( new String[] { "blop2" });
    new Tester ( new String[] { "blup" });

}

}

It outputs:

blep
ARGV [0] is blep

blep
ARGV [0] is blop2

blep
ARGV [0] is blup

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs