Sorry, my description wasnt good enough..

At the bottom of the message is the complete C source code.
The problem is where I put the big comment. At that line, I try to
create attribute accessor methods so that I can easily read and write
this variable ("myvar") from ruby land..

I thought rb_define_attr() works like:
class Test
  attr_reader :myvar
end
doesn't it?


#include "/usr/local/lib/ruby/1.8/i686-linux/ruby.h"
 
VALUE cRubyTest;
 
static VALUE t_init(VALUE self)
{
  VALUE str=rb_str_new2("default string");
  rb_define_attr(str, "myvar", 1, 1);           #  <------- PROBLEM!
  rb_iv_set(self, "@myvar", str);
  return self;
}

static VALUE t_add(VALUE self, VALUE anObject)
{
  VALUE arr;
  arr = rb_iv_get(self, "@arr");
  rb_ary_push(arr, anObject);
  rb_eval_string("puts \"testing...\"");
  return arr;
}
 
static VALUE t_change(VALUE self) {
  rb_iv_set(self, "@myvar", rb_str_new2("new string"));
  return self;
}

void Init_cRubyTest() {
  cRubyTest = rb_define_class("Test", rb_cObject);
  rb_define_method(cRubyTest, "initialize", t_init, 0);
  rb_define_method(cRubyTest, "add", t_add, 1);
  rb_define_method(cRubyTest, "change", t_change, 0);
}