Passing command line arguments to Ruby or JRuby script

hi all,

have just started working with ruby. i am building a jruby script which
has to branch to 20 threads and perform simultaneously. I am using
Java’s Native threads and then calling ruby scripts in each of them. But
am encountering serious problems as in :

Passing command - line arguments to the ruby script when it is called
from within the java thread( the arguments i need to pass are IP address
and an integer.).

Please help me in this regard…

Thanks in advance…

Swati

Swati Sharma wrote:

Please help me in this regard…

Can you post an example of what you’re doing? I’m not sure I understand.
You may want to look at the JRuby code used for spinning up new JRuby
instances in the same VM, since that allows passing parameters along
correctly.

  • Charlie

Charles Oliver N. wrote:

Swati Sharma wrote:

Please help me in this regard…

Can you post an example of what you’re doing? I’m not sure I understand.
You may want to look at the JRuby code used for spinning up new JRuby
instances in the same VM, since that allows passing parameters along
correctly.

  • Charlie

I am writing this jruby script for the SNMP management of the wireless
equipment installed in my campus. The way i have my script has 3 java
classes corresponding to the 3 main scripts that i have in ruby.Below i
paste the code for the rum method of one of my class categories (it just
calls the corresponding ruby script):

public void run() {
System.out.println(getName());
try {
ScriptEngineManager factory = new ScriptEngineManager();
// Create a JRuby engine.
ScriptEngine engine = factory.getEngineByName(“jruby”);
// Enable the JRuby script engine to invoke a script in a file.
Invocable inv = (Invocable) engine;
// Open a file reader on the external JRuby script file.
FileReader f = new
FileReader("/root/NetBeansProjects/java_thread_script_test/src/java_thread_script_test/cpeping.rb");
// Evaluate (execute) the script. Once evaluated, any functions
// in the script can be called with the invokeFunction method.
engine.eval(f);
}//end of try

Now, the problem is that i have made 3 classes and each of these classes
have to create 8 threads each (in java). so what i need is that i pass
on the IP Address and and an integer to the ruby script so that each
category (8 threads) can use the same ruby file instead of duplicating
the ruby scripts for individual threads.

I guess this much detail will be enough to seek your help. Waiting for
your reply…

Thanks…

Swati Sharma wrote:

Now, the problem is that i have made 3 classes and each of these classes
have to create 8 threads each (in java). so what i need is that i pass
on the IP Address and and an integer to the ruby script so that each
category (8 threads) can use the same ruby file instead of duplicating
the ruby scripts for individual threads.

Your best bet would be one of the following:

  • set a couple variables somewhere for each script run that they can use
    to retrieve the IP and integer
  • set up the ARGV array so it has the command-line parameters you want
    to pass through

There are also other ways to pass data into a scripting engine through
JSR223 that I won’t list here, but google for them and you’ll find some
good examples.

  • Charlie

Swati Sharma wrote:

Thanks Charlie. i made use of the ARGV array… now, when i am passing
my parameters from java they are perfectly visible within the run method
of the thread. But they are not reaching my method in the ruby script
which is doing all the processing. I am making use of the JSR223
scripting engine only. The invokeFunction method is the one that passes
the arguments from java to ruby. One possible reason that comes to my
mind is the absence of class in my script. is it neccessary to include a
class in the ruby script?

ARGV is only set up using command-line parameters for the top-level
“main” script you run. Any scripts that you invoke through BSF/JSR223
will require a separate step to set up ARGV there too. That could be as
simple as running some code to add entries to ARGV.

  • Charlie

Charles Oliver N. wrote:

Swati Sharma wrote:

Now, the problem is that i have made 3 classes and each of these classes
have to create 8 threads each (in java). so what i need is that i pass
on the IP Address and and an integer to the ruby script so that each
category (8 threads) can use the same ruby file instead of duplicating
the ruby scripts for individual threads.

Your best bet would be one of the following:

  • set a couple variables somewhere for each script run that they can use
    to retrieve the IP and integer
  • set up the ARGV array so it has the command-line parameters you want
    to pass through

There are also other ways to pass data into a scripting engine through
JSR223 that I won’t list here, but google for them and you’ll find some
good examples.

  • Charlie

Thanks Charlie. i made use of the ARGV array… now, when i am passing
my parameters from java they are perfectly visible within the run method
of the thread. But they are not reaching my method in the ruby script
which is doing all the processing. I am making use of the JSR223
scripting engine only. The invokeFunction method is the one that passes
the arguments from java to ruby. One possible reason that comes to my
mind is the absence of class in my script. is it neccessary to include a
class in the ruby script?