diff --git a/ChangeLog b/ChangeLog index e842fb9dc4..1be5693cac 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +Sat Jun 7 11:35:01 2014 Tanaka Akira + + * object.c (rb_mod_initialize_clone): Override Kernel#initialize_clone + to avoid an exception on Class.new.freeze.clone.to_s. + Reported by Andrew Grimm. [ruby-core:41858] [Bug #5828] + Sat Jun 7 06:03:11 2014 Benoit Daloze * ext/digest/digest.c (rb_digest_instance_equal): diff --git a/object.c b/object.c index 582e2e2121..57cbe68f1a 100644 --- a/object.c +++ b/object.c @@ -1701,6 +1701,16 @@ rb_mod_initialize(VALUE module) return Qnil; } +static VALUE +rb_mod_initialize_clone(VALUE clone, VALUE orig) +{ + VALUE ret; + ret = rb_obj_init_dup_clone(clone, orig); + if (OBJ_FROZEN(orig)) + rb_class_name(clone); + return ret; +} + /* * call-seq: * Class.new(super_class=Object) -> a_class @@ -3369,6 +3379,7 @@ Init_Object(void) rb_define_alloc_func(rb_cModule, rb_module_s_alloc); rb_define_method(rb_cModule, "initialize", rb_mod_initialize, 0); + rb_define_method(rb_cModule, "initialize_clone", rb_mod_initialize_clone, 1); rb_define_method(rb_cModule, "instance_methods", rb_class_instance_methods, -1); /* in class.c */ rb_define_method(rb_cModule, "public_instance_methods", rb_class_public_instance_methods, -1); /* in class.c */ diff --git a/test/ruby/test_class.rb b/test/ruby/test_class.rb index b40cab8050..963c497d2f 100644 --- a/test/ruby/test_class.rb +++ b/test/ruby/test_class.rb @@ -378,4 +378,10 @@ class TestClass < Test::Unit::TestCase assert_predicate(self.singleton_class, :singleton_class?, feature7609) assert_not_predicate(self.class, :singleton_class?, feature7609) end + + def test_freeze_to_s + assert_nothing_raised("[ruby-core:41858] [Bug #5828]") { + Class.new.freeze.clone.to_s + } + end end