Ruby , .
C, :
VALUE
force_class_name (VALUE klass, VALUE symbol_name)
{
rb_name_class(klass, SYM2ID(symbol_name));
return klass;
}
void
Init_my_extension ()
{
rb_define_method(rb_cClass, "force_class_name", force_class_name, 1);
}
This is a very difficult approach to the problem. Even if it works, it will not be guaranteed to work in different versions of ruby, since it relies on a non-API C function rb_name_class. I'm also not sure what behavior will be after Ruby starts using its own name hooks.
The code snippet for your use case will look like this:
require 'my_extension'
class MySuperclass
def self.inherited(subclass)
super
subclass.force_class_name(:FooBar)
end
end
source
share