Hi,

Class to class method transplanting could cause serious problem, but
it might be able to relax module to module transplanting.

							matz.

In message "Re: [ruby-core:34267] [Ruby 1.9-Feature#4254][Open] Allow method transplanting"
    on Sun, 9 Jan 2011 21:58:16 +0900, Jonas Pfenniger <redmine / ruby-lang.org> writes:

|Author: Jonas Pfenniger
|Status: Open, Priority: Normal
|Category: core, Target version: 1.9.3
|
|Is there a technical reason to not allow re-binding a method from one module to any other module ?
|
|  module M
|    def foo; "foo"; end
|  end
|
|  module N; end
|  N.send(:define_method, :foo, M.instance_method(:foo)) #=> should not raise
|
|It's like monkey-patching. Powerful, dangerous, but also really useful. It could allow different variations of method_wrap or alias_method_chain that are not possible right now.
|
|
|----------------------------------------
|http://redmine.ruby-lang.org
|[2 0001-method-transplanting-Allow-to-set-an-UnboundMethod-t.patch <text/plain (base64)>]
|From 0336ec334f7eb66d2cf05bd7a29d748780d6044e Mon Sep 17 00:00:00 2001
|From: Jonas Pfenniger <jonas / pfenniger.name>
|Date: Sun, 9 Jan 2011 00:13:57 +0000
|Subject: [PATCH] method transplanting: Allow to set an UnboundMethod to any module.
|
|It's time to grow up, remove the security nets.
|---
| proc.c |   11 -----------
| 1 files changed, 0 insertions(+), 11 deletions(-)
|
|diff --git a/proc.c b/proc.c
|index 7df2ec8..652b3e1 100644
|--- a/proc.c
|+++ b/proc.c
|@@ -1293,17 +1293,6 @@ 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 (FL_TEST(rclass, FL_SINGLETON)) {
|-		rb_raise(rb_eTypeError,
|-			 "can't bind singleton method to a different class");
|-	    }
|-	    else {
|-		rb_raise(rb_eTypeError,
|-			 "bind argument must be a subclass of %s",
|-			 rb_class2name(rclass));
|-	    }
|-	}
| 	rb_method_entry_set(mod, id, &method->me, noex);
|     }
|     else if (rb_obj_is_proc(body)) {
|-- 
|1.7.3.4