From: Brian Candler Date: 2009-10-20T20:50:19+09:00 Subject: Re: instance_eval with method proc Paul Carey wrote: > I'm trying to invoke a method defined in one class with self set to an > instance of a different class. If the method is in a common superclass, then you can make an UnboundMethod and bind it to the new object. e.g. >> class Foo; def foo; puts inspect; end; end => nil >> class Bar < Foo; end => nil >> b = Bar.new => # >> Foo.instance_method(:foo).bind(b).call # => nil >> But otherwise I don't think you can detach arbitrary methods from one class and apply them to an object of an unrelated class. You'd probably be best putting the relevant method(s) in a mixin, and then sharing that between the classes, or adding those methods to the singleton class of the individual object you're interested in. >> module Baz; def baz; puts "baz!"; end; end => nil >> s = "hello" => "hello" >> s.extend Baz => "hello" >> s.baz baz! => nil >> -- Posted via http://www.ruby-forum.com/.