On Mon, 23 Jun 2003 21:44:52 +0000, Shashank Date wrote:

> 
> "Shashank Date" <sdate / everestkc.net> wrote in message
>> I am still trying to figure out how to convert it into
>> a fully working C program (like all the other code examples).

Sorry.. I couldn't think of any good cutnpaste-example when I
wrote the text about rb_class_new_instance(). If anyone has ideas for
better examples they are welcome.

 
> To be more specific, I am on WIN XP Pro, using VC++ 6.0
> The following code does not work (it crashes and asks me if I
> want to send the error report to Microsoft. Probably because I
> do not know how "... to tell the Ruby's garbage collector that
> there is a new instance in town" )

There is 2 ways you can inform GC about the new instance.
#1   rb_define_variable(name, instance);
     by naming the instance, so it can be accessed by its
     Ruby-variable-name. Use this if you want to share an
     instance between Ruby and you extension/embedding code.

#2   rb_gc_register_address(&instance);
     don't want to assign a name to the instance.
     Use this if you want to keep some variables *private* to
     your extension/embedding code. 
     It cannot be accessed from a ruby-script.
 

> I have tested all the other programs and they all work fine so I don't
> think there is a problem with setting up my VisualStudio 6.0 environment.

OK.. nice to know.  I was worried that it wouldn't work on windows.
Perhaps you can add a short guide to rubygarden about how you have
setup'ed compilation on windows ?
 
> //-------------------------------------------------------------------
> #include <ruby.h>
> int main(int Argc, char **Argv) {
> 
>  int argc;
>  VALUE argv[2];
>  VALUE class;
>  VALUE instance;
> 
>  ruby_init();
> 
>  argv[0] = INT2FIX(42);
>  argv[1] = rb_str_new2("hello world");
>  argc = 2;
> 
>  /* ----- This does not work either:
> class = rb_path2class("TestClass");
>  instance = rb_class_new_instance(argc, argv, class);
> */
> 
>  class = rb_const_get(rb_cObject, rb_intern("TestClass"));
                                               ^^^^^^^^^

'TestClass' does not exist.. Try with a class-name which exists, perhaps:
String, Array, ..
Besides that you code looks ok :-)

>  instance = rb_funcall2(class, rb_intern("new"), argc, argv);
>  ruby_finalize();
>  return 0;
> 
> }
> 
> //-------------------------------------------------------------------



#include <ruby.h>
int main(int argc, char *argv[]) {
	int n;
	VALUE args[2];
	VALUE klass;
	VALUE instance;

	ruby_init();
	args[0] = INT2FIX(4);
	args[1] = rb_str_new2("ok");
	n = 2;
	klass = rb_path2class("Array");
	instance = rb_class_new_instance(n, args, klass);

	/* tell GC that there is a new instance in town. */
	rb_gc_register_address(&instance);

	rb_p(instance);

	ruby_finalize();
	return 0;
} 

> ./a.out
["ok", "ok", "ok", "ok"]


--
Simon Strandgaard