From: Roger Pack Date: 2008-12-02T04:53:58+09:00 Subject: Re: wish list item: Method#to_proc Ara Howard wrote: > On Nov 23, 2008, at 3:46 AM, Robert Dober wrote: > >> but I guess OP rather wanted >> M.i_m(:m).to_proc.call( self ) >> >> which on a quick thought seems impossible to have :-( > > na, just have to perform the late binding in the caller's stead > cfp:~ > ruby a.rb > # > 42 Wow that is dang cool. Next challenge: getting a method to transfer "easily" from one class to another [not directly related classes]. I guess you could do it if the method originated from a module--is that the only way? Ex: module AddToString def yoyo 'within string func' end end class String include AddToString end class Object # attempt to find and 'borrow' a method from another class # in 1.8, method_name should be a string, in 1.9, not sure def borrow_and_run_method(from_this, method_name_desired) module_where_found = nil for ancestor in from_this.ancestors do module_where_found ||= ancestor if ancestor.class == Module && ancestor.instance_methods.include?(method_name_desired) end raise unless module_where_found m = Module.new m.module_eval { include module_where_found} m.instance_methods.each{|method_name| m.module_eval{ undef_method(method_name) unless method_name == method_name_desired } } # todo: double check we're not overwriting anything... self.extend m self.send method_name_desired end end class NoYoYo; end print NoYoYo.new.borrow_and_run_method(String, 'yoyo') # prints 'within string func' -- Posted via http://www.ruby-forum.com/.