[前][次][番号順一覧][スレッド一覧]

ruby-changes:25367

From: shugo <ko1@a...>
Date: Fri, 2 Nov 2012 17:54:16 +0900 (JST)
Subject: [ruby-changes:25367] shugo:r37424 (trunk): * object.c (rb_mod_to_s): Module#{to_s,inspect}, when invoked on

shugo	2012-11-02 17:53:06 +0900 (Fri, 02 Nov 2012)

  New Revision: 37424

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=37424

  Log:
    * object.c (rb_mod_to_s): Module#{to_s,inspect}, when invoked on
      a refinement, returns a string in the format #<refinement:C@M>,
      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.

  Modified files:
    trunk/ChangeLog
    trunk/eval.c
    trunk/object.c
    trunk/test/ruby/test_refinement.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 37423)
+++ ChangeLog	(revision 37424)
@@ -1,3 +1,15 @@
+Fri Nov  2 17:52:12 2012  Shugo Maeda  <shugo@r...>
+
+	* object.c (rb_mod_to_s): Module#{to_s,inspect}, when invoked on
+	  a refinement, returns a string in the format #<refinement:C@M>,
+	  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  <sorah@t...>
 
 	* vm_dump.c (rb_vm_bugreport): Because of many log directories,
Index: object.c
===================================================================
--- object.c	(revision 37423)
+++ object.c	(revision 37424)
@@ -1336,6 +1336,9 @@
 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 @@
 
 	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("#<refinement:");
+
+	rb_str_concat(s, rb_inspect(refined_class));
+	rb_str_cat2(s, "@");
+	CONST_ID(id_defined_at, "__defined_at__");
+	defined_at = rb_attr_get(klass, id_defined_at);
+	rb_str_concat(s, rb_inspect(defined_at));
+	rb_str_cat2(s, ">");
+	return s;
+    }
     return rb_str_dup(rb_class_name(klass));
 }
 
Index: eval.c
===================================================================
--- eval.c	(revision 37423)
+++ eval.c	(revision 37424)
@@ -1218,7 +1218,7 @@
 {
     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 @@
 	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",
Index: test/ruby/test_refinement.rb
===================================================================
--- test/ruby/test_refinement.rb	(revision 37423)
+++ test/ruby/test_refinement.rb	(revision 37424)
@@ -622,4 +622,28 @@
   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("#<refinement:Fixnum@TestRefinement::Inspect::M>",
+                 Inspect::M.refinements[Fixnum].inspect)
+
+    c = Class.new
+    m = Module.new {
+      refine String do
+      end
+      refine c do
+      end
+    }
+    assert_equal("#<refinement:String@#{m.inspect}>",
+                 m.refinements[String].inspect)
+    assert_equal("#<refinement:#{c.inspect}@#{m.inspect}>",
+                 m.refinements[c].inspect)
+  end
 end

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]