From e6f1e3f49db1694116d286666ccaa026cad61f3e Mon Sep 17 00:00:00 2001 From: nobu Date: Mon, 25 Jun 2012 07:57:42 +0000 Subject: method transplanting * proc.c (rb_mod_define_method): allow method transplanting from a module to either class or module. [ruby-core:34267][Feature #4254] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@36214 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 5 +++++ proc.c | 3 ++- test/ruby/test_method.rb | 10 ++++++---- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/ChangeLog b/ChangeLog index 1297c3a3da..da7170902c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Mon Jun 25 16:57:38 2012 Nobuyoshi Nakada + + * 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 * io.c (is_popen_fork): check if fork and raise NotImplementedError if diff --git a/proc.c b/proc.c index 1298c21c93..bb7f4a1a25 100644 --- a/proc.c +++ b/proc.c @@ -1360,7 +1360,8 @@ rb_mod_define_method(int argc, VALUE *argv, VALUE mod) 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"); diff --git a/test/ruby/test_method.rb b/test/ruby/test_method.rb index ed04570e6a..e299b722ee 100644 --- a/test/ruby/test_method.rb +++ b/test/ruby/test_method.rb @@ -36,7 +36,7 @@ class TestMethod < Test::Unit::TestCase module M def func; end module_function :func - def meth; end + def meth; :meth end end def mv1() end @@ -230,9 +230,11 @@ class TestMethod < Test::Unit::TestCase 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 -- cgit v1.2.3