From b14e2b4401db4746cc45ae9e4ce6350f7594f84f Mon Sep 17 00:00:00 2001 From: shugo Date: Fri, 2 Nov 2012 08:53:06 +0000 Subject: [PATCH] * object.c (rb_mod_to_s): Module#{to_s,inspect}, when invoked on a refinement, returns a string in the format #, where C is a refined class and M is a module at which the refinemet is defined. * eval.c (rb_mod_refine): store information on a refinement for the above change. * test/ruby/test_refinement.rb: related test. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37424 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 12 ++++++++++++ eval.c | 4 +++- object.c | 16 ++++++++++++++++ test/ruby/test_refinement.rb | 24 ++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index 00b38f4b30..42d0967085 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,15 @@ +Fri Nov 2 17:52:12 2012 Shugo Maeda + + * object.c (rb_mod_to_s): Module#{to_s,inspect}, when invoked on + a refinement, returns a string in the format #, + where C is a refined class and M is a module at which the refinemet + is defined. + + * eval.c (rb_mod_refine): store information on a refinement for the + above change. + + * test/ruby/test_refinement.rb: related test. + Fri Nov 2 16:57:52 2012 Shota Fukumori * vm_dump.c (rb_vm_bugreport): Because of many log directories, diff --git a/eval.c b/eval.c index 08d20d2204..21c33137f0 100644 --- a/eval.c +++ b/eval.c @@ -1218,7 +1218,7 @@ rb_mod_refine(VALUE module, VALUE klass) { NODE *cref = rb_vm_cref(); VALUE mod; - ID id_refinements, id_refined_class; + ID id_refinements, id_refined_class, id_defined_at; VALUE refinements; if (!rb_block_given_p()) { @@ -1236,6 +1236,8 @@ rb_mod_refine(VALUE module, VALUE klass) mod = rb_module_new(); CONST_ID(id_refined_class, "__refined_class__"); rb_ivar_set(mod, id_refined_class, klass); + CONST_ID(id_defined_at, "__defined_at__"); + rb_ivar_set(mod, id_defined_at, module); rb_define_singleton_method(mod, "method_added", refinement_module_method_added, 1); rb_define_singleton_method(mod, "include", diff --git a/object.c b/object.c index f3e5ec241b..1b020beaa3 100644 --- a/object.c +++ b/object.c @@ -1336,6 +1336,9 @@ rb_obj_cmp(VALUE obj1, VALUE obj2) static VALUE rb_mod_to_s(VALUE klass) { + ID id_refined_class, id_defined_at; + VALUE refined_class, defined_at; + if (FL_TEST(klass, FL_SINGLETON)) { VALUE s = rb_usascii_str_new2("#<"); VALUE v = rb_iv_get(klass, "__attached__"); @@ -1353,6 +1356,19 @@ rb_mod_to_s(VALUE klass) return s; } + CONST_ID(id_refined_class, "__refined_class__"); + refined_class = rb_attr_get(klass, id_refined_class); + if (!NIL_P(refined_class)) { + VALUE s = rb_usascii_str_new2("#"); + return s; + } return rb_str_dup(rb_class_name(klass)); } diff --git a/test/ruby/test_refinement.rb b/test/ruby/test_refinement.rb index 4a0e6d1327..eee66105b1 100644 --- a/test/ruby/test_refinement.rb +++ b/test/ruby/test_refinement.rb @@ -622,4 +622,28 @@ class TestRefinement < Test::Unit::TestCase def test_symbol_to_proc assert_equal("foo", SymbolToProc::M.call_foo) end + + module Inspect + module M + refine Fixnum do + end + end + end + + def test_inspect + assert_equal("#", + Inspect::M.refinements[Fixnum].inspect) + + c = Class.new + m = Module.new { + refine String do + end + refine c do + end + } + assert_equal("#", + m.refinements[String].inspect) + assert_equal("#", + m.refinements[c].inspect) + end end