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.demon.co.uk
RSI Information: http://www.objmedia.demon.co.uk/rsi.html
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.demon.co.uk
RSI Information: http://www.objmedia.demon.co.uk/rsi.html
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
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.demon.co.uk
RSI Information: http://www.objmedia.demon.co.uk/rsi.html
Ruby 1.8.1 (2003-12-25) [i386-mswin32]
Stephen
···
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'?
--
Stephen Kellett
Object Media Limited http://www.objmedia.demon.co.uk
RSI Information: http://www.objmedia.demon.co.uk/rsi.html
This was an interesting suggestion, but unfortunately doesn't fix the problem.
Stephen
···
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?
--
Stephen Kellett
Object Media Limited http://www.objmedia.demon.co.uk
RSI Information: http://www.objmedia.demon.co.uk/rsi.html
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