From: Jacob Fugal Date: 2008-11-28T02:39:44+09:00 Subject: [ruby-core:20141] Re: Ruby class variable access from C On Thu, Nov 27, 2008 at 9:27 AM, Christopher Thompson 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". > > 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. :) To elaborate on Matz' suggestion, I'd do something like: VALUE klass = rb_const_get("Myclass"); VALUE myvar = rb_cvar_get(klass, "myvar"); Excuse any mistake in my usage of either of those methods, I haven't actually used them in quite some time and didn't bother looking at the documentation. :) Jacob Fugal