Ok, now I think I see the problem. Your
VALUE test_var;
is a sort of "shared" value among every instance of the test class. This is an error of course, since it is overwritten as new instances are created or method set is called. So you can have just a single instance and a value shared among every instance.
Of course you are doing something wrong: ruby has to provide context and a way to retrieve it, likely the proto for the get function must have at least VALUE self as argument, like set. The value can't be stored into global or static local variable: it must be someway stored into the "context" of the object. In order to know how to do so, I need a fast tutorial about ruby ext. In the meantime try to read deeper here.
In particular, focus your attention on "Accessing Variables" and how you define instance variables.
I have done this, that seems to work; you could like to work on it to achieve your extension purpose(s) (I've renamed something, and fixed something else; I've also removed the INT2NUM and NUM2INT stuff, you can put it back at your need)
#include <stdlib.h>
#include <ruby.h>
VALUE TestClass;
VALUE SCOPE;
VALUE set(VALUE, VALUE);
VALUE get(VALUE);
VALUE set(VALUE self, VALUE val) {
(void)rb_iv_set(self, "@val", val);
return Qnil;
}
VALUE get(VALUE self) {
return rb_iv_get(self, "@val");
}
void Init_RubyTest()
{
SCOPE = rb_define_module("RubyTest");
TestClass = rb_define_class_under(SCOPE, "TestClass", rb_cObject);
rb_define_method(TestClass, "set=", set, 1);
rb_define_method(TestClass, "get", get, 0);
}
This question can't be answered fully if we don't know how "C extension" (I suppose, to Ruby?) works, and sincerely I don't know.
A "global" variable declared static is local to the file where it is defined and can't be accessed from outside, i.e. it is global inside that file, but it is not a global for all the linked files.
func1 can access bar, indeed; it can't just because the symbol is not known until it is declared (for the same reason func1 can't call func2, or at least compiler gives a warning for the missing prototype, then the code to func2 will be found anyway), but anyway, once the symbol is known, it can be accessed. On the contrary, those variables bar and foo can't be seen from outside (and so are not global) since the symbols foo and bar are not visible.
If this code is supposed to be compiled as a shared object or a static library, foo and bar won't be visible by the code that links the shared object / static library.