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

ruby-changes:24163

From: nobu <ko1@a...>
Date: Mon, 25 Jun 2012 16:57:53 +0900 (JST)
Subject: [ruby-changes:24163] nobu:r36214 (trunk): method transplanting

nobu	2012-06-25 16:57:42 +0900 (Mon, 25 Jun 2012)

  New Revision: 36214

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

  Log:
    method transplanting
    
    * proc.c (rb_mod_define_method): allow method transplanting from a
      module to either class or module.  [ruby-core:34267][Feature #4254]

  Modified files:
    trunk/ChangeLog
    trunk/proc.c
    trunk/test/ruby/test_method.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 36213)
+++ ChangeLog	(revision 36214)
@@ -1,3 +1,8 @@
+Mon Jun 25 16:57:38 2012  Nobuyoshi Nakada  <nobu@r...>
+
+	* proc.c (rb_mod_define_method): allow method transplanting from a
+	  module to either class or module.  [ruby-core:34267][Feature #4254]
+
 Mon Jun 25 15:42:00 2012  Nobuyoshi Nakada  <nobu@r...>
 
 	* io.c (is_popen_fork): check if fork and raise NotImplementedError if
Index: proc.c
===================================================================
--- proc.c	(revision 36213)
+++ proc.c	(revision 36214)
@@ -1360,7 +1360,8 @@
     if (rb_obj_is_method(body)) {
 	struct METHOD *method = (struct METHOD *)DATA_PTR(body);
 	VALUE rclass = method->rclass;
-	if (rclass != mod && !RTEST(rb_class_inherited_p(mod, rclass))) {
+	if (rclass != mod && !RB_TYPE_P(rclass, T_MODULE) &&
+	    !RTEST(rb_class_inherited_p(mod, rclass))) {
 	    if (FL_TEST(rclass, FL_SINGLETON)) {
 		rb_raise(rb_eTypeError,
 			 "can't bind singleton method to a different class");
Index: test/ruby/test_method.rb
===================================================================
--- test/ruby/test_method.rb	(revision 36213)
+++ test/ruby/test_method.rb	(revision 36214)
@@ -36,7 +36,7 @@
   module M
     def func; end
     module_function :func
-    def meth; end
+    def meth; :meth end
   end
 
   def mv1() end
@@ -230,9 +230,11 @@
       Module.new.module_eval {define_method(:foo, Base.instance_method(:foo))}
     end
 
-    assert_raise(TypeError) do
-      Class.new.class_eval {define_method(:meth, M.instance_method(:meth))}
-    end
+    feature4254 = '[ruby-core:34267]'
+    m = Module.new {define_method(:meth, M.instance_method(:meth))}
+    assert_equal(:meth, Object.new.extend(m).meth, feature4254)
+    c = Class.new {define_method(:meth, M.instance_method(:meth))}
+    assert_equal(:meth, c.new.meth, feature4254)
   end
 
   def test_super_in_proc_from_define_method

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

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