From: Christopher Thompson Date: 2008-11-28T01:27:45+09:00 Subject: [ruby-core:20139] Re: Ruby class variable access from C _why wrote: > On Thu, Nov 27, 2008 at 04:49:32AM +0900, Christopher Thompson wrote: >> I think I can get it by doing: >> VALUE myvar = rb_eval_string("Myclass::myvar"); >> but surely there's a better way? I'm thinking something like >> rb_cvar_get() but that requires that I pass in a VALUE and all I have is >> a string at this point, "Myclass". > > If you create the class in your C extension, you'll get a VALUE > back. > > VALUE cMyclass; > > void > Init_myext() > { > cMyclass = rb_define_class("Myclass", rb_cObject); > } > > VALUE > much_much_later(VALUE self) > { > VALUE myvar = rb_cvar_get(cMyclass, rb_intern("@@myvar")); > } > > _why > Ahh, but I'm not creating it in the C extension, for reasons that won't be immediately obvious and possibly should be refactored. Matz suggested rb_const_get and rb_eval_string. rb_eval_string is what I'm already doing, so I'll look into rb_const_get. But thanks, your approach would work for most sane extensions. :)