Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Passing command line arguments to Ruby or JRuby script.

Reply
Thread Tools

Passing command line arguments to Ruby or JRuby script.

 
 
Swati Sharma
Guest
Posts: n/a
 
      04-02-2008
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
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Charles Oliver Nutter
Guest
Posts: n/a
 
      04-02-2008
Swati Sharma wrote:
> 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...


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

 
Reply With Quote
 
 
 
 
Swati Sharma
Guest
Posts: n/a
 
      04-03-2008
Charles Oliver Nutter 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..
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Charles Oliver Nutter
Guest
Posts: n/a
 
      04-03-2008
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

 
Reply With Quote
 
Swati Sharma
Guest
Posts: n/a
 
      04-07-2008
Charles Oliver Nutter 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?
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Charles Oliver Nutter
Guest
Posts: n/a
 
      04-07-2008
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
passing command line arguments to executable mcanjo Python 11 04-06-2010 08:15 PM
Not Passing On Command Line Arguments Dylan Ruby 1 07-27-2009 04:32 PM
passing command line arguments thru ant jimgardener Java 1 10-20-2008 05:30 PM
Passing arguments to a command line from a python script =?iso-8859-1?q?Luis_M._Gonz=E1lez?= Python 6 03-20-2007 03:18 AM
Run Unix shell command $ parse command line arguments in python rkoida@yahoo.com Python 4 04-23-2005 04:42 AM



Advertisments