You can abuse (({Module#initialize_copy})) to change the superclass of a class, because of this line of code in (({rb_mod_init_copy})):
RCLASS_SUPER(clone) = RCLASS_SUPER(orig);
(({Class#initialize_copy})) does perform some checks to stop you calling (({#initialize})) on an already initialized class, but this can be subverted by redefining it to call (({super})) from Ruby-land.
Here's an example:
class Class
def initialize_copy(*)
super
end
def superclass=(klass)
initialize_copy(Class.new(klass))
end
This issue was solved with changeset r38364.
Charlie, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.
object.c (Init_Object): use rb_mod_init_copy for Class#initialize_copy
class.c (rb_class_init_copy): rename to class_init_copy_check, performs type
checks on arguments to prevent reinitialization of initialized class [ruby-core:50869] [Bug #7557]
class.c (rb_mod_init_copy): use class_init_copy_check if receiver is T_CLASS
test/ruby/test_class.rb (class TestClass): related test
This issue was solved with changeset r38507.
Charlie, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.
merge revision(s) 38364,38366: [Backport #7557]
* object.c (Init_Object): use rb_mod_init_copy for Class#initialize_copy
* class.c (rb_class_init_copy): rename to class_init_copy_check, performs type
checks on arguments to prevent reinitialization of initialized class
[ruby-core:50869] [Bug #7557]
* class.c (rb_mod_init_copy): use class_init_copy_check if receiver is T_CLASS
* test/ruby/test_class.rb (class TestClass): related test