Problem using ruby_options() - help!

  • Thread starter Stephen Kellett
  • Start date
S

Stephen Kellett

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
 
S

Sam Roberts

Quoteing (e-mail address removed), 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
 
N

nobu.nokada

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'?
 
S

Stephen Kellett

Sam Roberts said:
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
 
S

Stephen Kellett

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
 
N

nobu.nokada

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,880
Messages
2,569,944
Members
46,246
Latest member
RosalieMar

Latest Threads

Top