Passing command line arguments to Ruby or JRuby script.

Discussion in 'Ruby' started by Swati Sharma, Apr 2, 2008.

  1. Swati Sharma

    Swati Sharma Guest

    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/.
     
    Swati Sharma, Apr 2, 2008
    #1
    1. Advertising

  2. 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
     
    Charles Oliver Nutter, Apr 2, 2008
    #2
    1. Advertising

  3. Swati Sharma

    Swati Sharma Guest

    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/.
     
    Swati Sharma, Apr 3, 2008
    #3
  4. 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
     
    Charles Oliver Nutter, Apr 3, 2008
    #4
  5. Swati Sharma

    Swati Sharma Guest

    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/.
     
    Swati Sharma, Apr 7, 2008
    #5
  6. 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 Nutter, Apr 7, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    1,716
    Terry Hancock
    Apr 23, 2005
  2. =?iso-8859-1?q?Luis_M._Gonz=E1lez?=

    Passing arguments to a command line from a python script

    =?iso-8859-1?q?Luis_M._Gonz=E1lez?=, Mar 19, 2007, in forum: Python
    Replies:
    6
    Views:
    2,680
    =?iso-8859-1?q?Luis_M._Gonz=E1lez?=
    Mar 20, 2007
  3. jimgardener
    Replies:
    1
    Views:
    5,328
    Donkey Hottie
    Oct 20, 2008
  4. mcanjo
    Replies:
    11
    Views:
    526
    Bror Johansson
    Apr 6, 2010
  5. Jayson Williams
    Replies:
    28
    Views:
    344
    Mohit Sindhwani
    Nov 30, 2008
Loading...

Share This Page