Help embedding Ruby in Visual C++ project

S

stevewong4

Hi,
I am trying to embed Ruby within a Visual C++ project, but I am having
trouble loading my script file.

How do I tell the Ruby interpreter where to look for my script file?

If I use ruby_options(), then my program hangs. So, I am trying to
pass the "-S" flag to Ruby, and I have set RUBYPATH to the directory
where "test.rb" lives.

Note: I am able to load the script if I put the script in the same
directory as my source files (which isn't the same directory as the
built C++ .exe). I do not understand why this works. Ideally, I would
like to tell the Ruby interpreter where to find my "test.rb" script.
How do I do this? Thanks in advance for your help.

Here is a code snippet:
int state;
int rargc=2;
char *rargv[2], rarg1[MAX_PATH]

strcpy(rarg1, "-S");
rargv[0] = "ruby";
rargv[1] = rarg1;

NtInitialize(&rargc, (char ***)&rargv);

ruby_init();
ruby_init_loadpath();

rb_load_protect(rb_str_new2("test.rb"), Qfalse, &state);
 
T

ts

s> ruby_init_loadpath();

Well, ruby use the variable $LOAD_PATH to know where it must search a
library. This is this variable which is initialized when you call
rb_init_loadpath() and by default (with $SAFE == 0) it add `.', the
current directory, in it. This is why it find the script when it's in the
current directory.

You can use, ruby_incpush() if you want to add directories to this
variable.

moulon% grep ruby_incpush *.h
intern.h:void ruby_incpush _((const char*));
moulon%



Guy Decoux
 
S

Stephen Kellett

In message said:
Hi,
I am trying to embed Ruby within a Visual C++ project, but I am having
trouble loading my script file.

How do I tell the Ruby interpreter where to look for my script file?

When we want to test something in Ruby using C we have a test harness.
I've stripped out the non-relevant code to reveal this. Hope it helps

Stephen

int main(int argc, char* argv[])
{
NtInitialize(&argc, &argv);

ruby_init();

ruby_script("embedded");

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

ruby_set_argv((sizeof(args) / sizeof(args[0])) - 1, args);

char *options[] =
{
"",
NULL
};

ruby_options(0, options); // required to setup TK properly

ruby_init_loadpath();


rb_load_file("d:\\ruby_1_8_2_15\\samples\\tk\\tkhello.rb");

ruby_run();

return 0;
}
 

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,882
Messages
2,569,950
Members
46,280
Latest member
Gilberthep

Latest Threads

Top