Quote:
Originally Posted by Neil S
Yeah, he's using global variables... which unsurprisingly have global scope.
If he wanted variables to be class scoped, then he needed them stored with the class.
Hey Neil, that was actually me asking the question. I haven't used C in 10 years and I'm apparently really rusty. I got things to work by using all ruby variables like:
Code:
VALUE TestClass;
VALUE SCOPE;
VALUE set(self, val);
VALUE get();
VALUE set(VALUE self, VALUE val) {
rb_iv_set(self, "test_var", val);
return Qnil;
}
VALUE get(VALUE self) {
return rb_iv_get(self, "test_var");
}
void Init_scope()
{
SCOPE = rb_define_module("SCOPE");
TestClass = rb_define_class_under(SCOPE, "TestClass", rb_cObject);
rb_define_method(TestClass, "set=", set, 1);
rb_define_method(TestClass, "get", get, 0);
rb_define_variable("@test_var", 0);
}
But I still don't see how to make a variable have scope that only applies to a class if I write all the code without the ruby helpers. If I declare a C variable in the init method, the other functions don't have access to it. And if I declare it externally it is global. What is the obvious thing that I am missing?