From: Charles Oliver Nutter Date: 2009-01-19T06:32:17+09:00 Subject: [ruby-core:21422] Re: Proposal: Module#copy_method Yehuda Katz wrote: > I'd like it to be possible to copy methods from one module to another. > The rationale is that this feature would make it possible to modify > existing methods without being forced to resort to aliasing. Here's an > implementation that works on 1.8 and 1.9: Here's a pure-ruby version that works on an unmodified JRuby: require 'jruby' class Module def copy_method(mod, sym) # get JRuby DynamicMethod object from RubyMethod org.jruby.RubyMethod.field_reader :method ruby_method = mod.instance_method(sym) real_method = JRuby.reference(ruby_method).method # native methods may have memory/object layout needs raise "can't copy native method" if real_method.native? # Add it directly to the JRuby RubyModule self real_modcls = JRuby.reference(self) real_modcls.add_method(sym.to_s, real_method) end end - Charlie