I am looking into example from Programming Ruby 1.9. Is it possible to create instance variables not exposed to Ruby, visible only in C - for example to initialize C-structure in t_init and use it in t_add ?
If I declare the structure or variable below the id_push, it is class structure/variable and I need instance.
Is it possible to achieve without using Data_Wrap_Struct/Data_Get_Struct or rb_iv_set/rb_iv_get, as the instance variable is not needed in Ruby, it is enough to be visible from C only?
#include "ruby.h"
static ID id_push;
// how to define instance variables here?
static VALUE t_init(VALUE self) {
VALUE arr;
arr = rb_ary_new();
rb_iv_set(self, "@arr", arr);
return self;
}
static VALUE t_add(VALUE self, VALUE obj) {
VALUE arr;
arr = rb_iv_get(self, "@arr");
rb_funcall(arr, id_push, 1, obj);
return arr;
}
VALUE cTest;
void Init_my_test() {
cTest = rb_define_class("MyTest", rb_cObject);
rb_define_method(cTest, "initialize", t_init, 0);
rb_define_method(cTest, "add", t_add, 1); id_push = rb_intern("push");
}