From: "Suraj N. Kurapati" Date: 2008-04-06T01:13:49+09:00 Subject: Re: unable to set $0 from C extension Suraj N. Kurapati wrote: > I tried several approaches to set $0 from my C extension but none > have succeeded in providing a modifiable $0 to Ruby code. In > particular, any modifications to $0 from Ruby code are raising a > "RuntimeError: $0 not initialized". I dug around the Ruby source code some more and found the ruby_prog_init() function which is called by ruby_init() to define $0 as a hooked variable. So the solution was to simply redefine the hooked handler for $0 after ruby_init() was called. The new handler would treat $0 as a pure Ruby value and would allow it to be modified without restriction: static VALUE gProgName = Qnil; static void setProgName(VALUE name, ID id) { gProgName = rb_obj_as_string(name); rb_obj_taint(gProgName); } main() { // ... ruby_init(); // // override Ruby's hooked handlers for $0 so that $0 can be // treated as pure Ruby value and modified without restriction // rb_define_hooked_variable("$0", &gProgName, 0, setProgName); rb_define_hooked_variable("$PROGRAM_NAME", &gProgName, 0, setProgName); // ... } Cheers.