From: Robert Klemme Date: 2005-04-01T20:29:44+09:00 Subject: Re: how to simulate self-style delegation "itsme213" schrieb im Newsbeitrag news:y463e.17923$1H3.10469@tornado.texas.rr.com... > I have classes A and B. > > class A > def x > "A::x " > end > def xy > self.x + "A::y" > end > end > > class B > def initialize a > @a = a > end > def x > "B::x " > end > def method_missing ??? > ?? SELF-style delegate to @a > end > end > > I want true SELF-style delegation from objects of class B to A. i.e. self > calls on an A are in the context of the delegating B (if any). > > a = A.new > a.xy #=> "A::x A::y" > > b = B.new a > > b.xy #=> "B::x A::y" > # calls b.method missing > # calls a.xy > # calls b.x #=> "B::x" > # calls a.y #=> "A::y" > # return "B::x A::y" > > How can I do this? From all that I know you can't because you cannot do any of these: - bind an instance method of A to B (unless B is a superclass of A) - convert a method to a proc that is usable with instance_eval. The question is: does it make sense to do that? Kind regards robert Below's my test code. class A def x "A::x " end def xy self.x + "A::y" end end class B def initialize a @a = a end def x "B::x " end # def respond_to?(*x) true end def method_missing(sym, *args, &bl) ppp = @a.method(sym).to_proc # p ppp.arity class<