[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Problem using ruby_options() - help!

Stephen Kellett

12/7/2004 7:53:00 PM

Hello everyone,

I'm trying to call a Ruby program from C and pass it some arguments.
Here is a trivial program that demonstrates the problem. The Ruby
documentation states that you can set the Ruby arguments using
ruby_options. I know that if I pass no arguments it waits forever
listening to STDIN. Thats fine. The problem is that when I do pass
arguments Ruby just dies - no crash, nothing, just a silent exit.

Any idea what I'm doing wrong? Should I be using a different call to set
the arguments?

I'm using Ruby 1.8.1

int main(int argc, char* argv[])
{
NtInitialize(&argc, &argv); // this bit need for Windows

ruby_init();
ruby_script("embedded");

char *rubyArgs[] =
{
"arg1",
"arg2",
"arg3",
};

// this function should set the arguments to the ruby program
// however Ruby silently dies at this point

ruby_options(sizeof(rubyArgs) / sizeof(rubyArgs[0]), rubyArgs);

// the code never gets here

rb_load_file("e:\\doTheWork.rb");
ruby_run();

return 0;
}

Stephen
--
Stephen Kellett
Object Media Limited http://www.objmedia.d...
RSI Information: http://www.objmedia.d.../rsi.html
6 Answers

Sam Roberts

12/7/2004 9:53:00 PM

0

Quoteing snail@objmedia.demon.co.uk, on Wed, Dec 08, 2004 at 05:02:32AM +0900:
> Hello everyone,
>
> I'm trying to call a Ruby program from C and pass it some arguments.
> Here is a trivial program that demonstrates the problem. The Ruby
> documentation states that you can set the Ruby arguments using
> ruby_options. I know that if I pass no arguments it waits forever
> listening to STDIN. Thats fine. The problem is that when I do pass
> arguments Ruby just dies - no crash, nothing, just a silent exit.
>
> Any idea what I'm doing wrong? Should I be using a different call to set
> the arguments?
>
> I'm using Ruby 1.8.1
>
> int main(int argc, char* argv[])
> {
> NtInitialize(&argc, &argv); // this bit need for Windows
>
> ruby_init();
> ruby_script("embedded");
>
I don't know anything about ruby here.. but I know the standard format
for ARGV arrays. perhaps you should null terminate rubyArgs?

> char *rubyArgs[] =
> {
> "arg1",
> "arg2",
> "arg3",
NULL
> };

>
> // this function should set the arguments to the ruby program
> // however Ruby silently dies at this point
>

ruby_options(sizeof(rubyArgs) / sizeof(rubyArgs[0]) - 1, rubyArgs);


Cheers,
Sam

>
> // the code never gets here
>
> rb_load_file("e:\\doTheWork.rb");
> ruby_run();
>
> return 0;
> }
>
> Stephen
> --
> Stephen Kellett
> Object Media Limited http://www.objmedia.d...
> RSI Information: http://www.objmedia.d.../rsi.html
>


nobu.nokada

12/8/2004 5:21:00 AM

0

Hi,

At Wed, 8 Dec 2004 05:02:32 +0900,
Stephen Kellett wrote in [ruby-talk:122839]:
> I'm trying to call a Ruby program from C and pass it some arguments.
> Here is a trivial program that demonstrates the problem. The Ruby
> documentation states that you can set the Ruby arguments using
> ruby_options. I know that if I pass no arguments it waits forever
> listening to STDIN. Thats fine. The problem is that when I do pass
> arguments Ruby just dies - no crash, nothing, just a silent exit.

What I got from your example (without NtIntialize) is:

$ ./x
arg1: No such file or directory -- arg2 (LoadError)

> Any idea what I'm doing wrong? Should I be using a different call to set
> the arguments?
>
> I'm using Ruby 1.8.1

What's the exact version you are using, the result of `ruby -v'?

--
Nobu Nakada


Stephen Kellett

12/8/2004 10:41:00 AM

0

In message
<200412080520.iB85Ker4006311@sharui.nakada.niregi.kanuma.tochigi.jp>,
nobu.nokada@softhome.net writes
>> I'm using Ruby 1.8.1
>
>What's the exact version you are using, the result of `ruby -v'?

Ruby 1.8.1 (2003-12-25) [i386-mswin32]

Stephen
--
Stephen Kellett
Object Media Limited http://www.objmedia.d...
RSI Information: http://www.objmedia.d.../rsi.html

Stephen Kellett

12/8/2004 10:41:00 AM

0

In message <20041207215248.GB8458@ensemble.local>, Sam Roberts
<sroberts@uniserve.com> writes
>I don't know anything about ruby here.. but I know the standard format
>for ARGV arrays. perhaps you should null terminate rubyArgs?

This was an interesting suggestion, but unfortunately doesn't fix the
problem.

Stephen
--
Stephen Kellett
Object Media Limited http://www.objmedia.d...
RSI Information: http://www.objmedia.d.../rsi.html

Stephen Kellett

12/8/2004 8:38:00 PM

0

>listening to STDIN. Thats fine. The problem is that when I do pass
>arguments Ruby just dies - no crash, nothing, just a silent exit.

I've got a solution.

I was originally making the following call which was dying.

char *rubyArgs[] =
{
"arg1",
"arg2",
"arg3",
};

ruby_options(sizeof(rubyArgs) / sizeof(rubyArgs[0]), rubyArgs);

I built the ruby source and stepped through ruby_options() - it did
various strange things like calling ruby_script(args[0]) - thus
overwriting the value I'd already set, then later is message about with
args[1] and called ruby_script() on that messed about value, finally it
tried to load a file with the value of args[1]. That action failed and
ruby bailed out with an internal load error. Hence my problem.

Whilst doing the above I noticed the call ruby_set_argv(). A few tests
later and I conclude that the correct way to pass arguments to a Ruby
program, so far as I can tell is using

ruby_set_argv(count, args);

Hope that helps someone, most likely using Google Groups to get
themselves out of this hole. Thanks to those that replied trying to help
me.

Stephen
--
Stephen Kellett
Object Media Limited http://www.objmedia.d...
RSI Information: http://www.objmedia.d.../rsi.html

nobu.nokada

12/9/2004 8:57:00 AM

0

Hi,

At Thu, 9 Dec 2004 05:47:29 +0900,
Stephen Kellett wrote in [ruby-talk:122996]:
> Whilst doing the above I noticed the call ruby_set_argv(). A few tests
> later and I conclude that the correct way to pass arguments to a Ruby
> program, so far as I can tell is using
>
> ruby_set_argv(count, args);

ruby_set_argv() sets the arguments for the script, while
ruby_options() accepts the arguments to the ruby interpreter,
including the script name and them.

--
Nobu Nakada