You should use the rb_define_readonly_variable function from the C extension, for example:
VALUE var; void Init_my_extension(void) { var = Qnil;
Then, when you try to do this from ruby:
$var = 123
You will get an error message.
NameError: $var is a read-only variable
source share