From: Brian Candler Date: 2009-10-20T21:02:32+09:00 Subject: Re: instance_eval with method proc Brian Candler wrote: > 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 >>> ... and of course, object b usually has the 'foo' method already inherited from the parent, in which case b.send(:foo) is sufficient. But if 'foo' has been overridden, it can still be useful to bind this way. >> class Foo; def foo; puts "foo from parent"; end; end => nil >> class Bar < Foo; def foo; puts "foo from child"; end; end => nil >> b = Bar.new => # >> b.send(:foo) foo from child => nil >> m = Foo.instance_method(:foo) => # >> m.bind(b).call foo from parent => nil -- Posted via http://www.ruby-forum.com/.