nobu.nokada / softhome.net writes:

> --Multipart_Fri_Feb_28_19:22:32_2003-1
> Content-Type: text/plain; charset=US-ASCII
> 
> Hi,
> 
> At Fri, 28 Feb 2003 18:02:53 +0900,
> Friedrich Dominicus wrote:
> > assume I have a ruby file like that
> > 
> > 
> > class SomeClass
> > 
> >         @@some_var = 0;
> > 
> > end
> > 
> > 
> > Now I want to create an this Object from C and access 
> > @@some_var
> 
> rb_cvar_get() or rb_cv_get().  The former accepts ID and name
> for the latter.
Well it's a bit different (as I found out myself short after my
posting)


> 
> 
> --Multipart_Fri_Feb_28_19:22:32_2003-1
> Content-Type: text/c-src; charset=US-ASCII
> Content-Disposition: attachment; filename="someclass.c"
> Content-Transfer-Encoding: 7bit
> 
> #include "ruby.h"
> 
> static VALUE
> some_s_var(VALUE klass)
> {
>     return rb_cv_get(klass, "@@some_var");
> }
> 
> static VALUE
> some_var(VALUE self)
> {
>     return some_s_var(CLASS_OF(self));
> }
> 
> void
> Init_someclass()
> {
>     VALUE someclass = rb_const_get(rb_cObject, rb_intern("SomeClass"));
>     rb_define_method(someclass, "some_var", some_var, 0);
>     rb_define_singleton_method(someclass, "some_var", some_s_var, 0);
> }
This is not what I wanted ot do I just wanted to use the Ruby stuff
directly no C extension library.

Here's what I have come up with

class T1
  @@some_var = [ "String1",
    "String2", 
    "String3"]
end

/*  Generated header
 *  File Name : t1.c
 *
 *  Created   : 2003-02-28 09:57:47 frido
 *  Author    : Friedrich Dominicus
 *  Time-stamp: <>
*/

#include <ruby.h>
#include <stdio.h>


int main (void){
  int i;
  VALUE obj;
  VALUE klass;
  ruby_init(); /* embed Ruby into this program! */
  /* uby_script("embed");
     rb_load_file("t1.rb"); */ 
  obj = rb_eval_string("load 't1.rb'");
        /* question why does rb_load_file does not work? */
  klass =  rb_const_get(rb_cObject, rb_intern("T1"));
        /* why does this work? Well my understanding ist that I loaded
the file into the embedded ruby, after that T1 is known, so I can look
it up. The problem I now is why rb_load_file does not work the same
way */
  obj = rb_cv_get(klass, "@@some_var");
  
  
  for (i = 0; i < RARRAY(obj)->len; ++i){
    printf("obj[%d] = %s\n", i, STR2CSTR(rb_ary_entry(obj,i)));
  }
  return 0;
}

Comments are welcome!

Anyway thanks for your answer.

Regards
Friedrich