From: Massimiliano Mirra Date: 2002-07-09T22:41:24+09:00 Subject: Re: self_parent On Tue, Jul 09, 2002 at 03:51:24PM +0900, Tom Sawyer wrote: > class A > def initialize(x) > x.call > end > def display > puts 'hello' > end > end > > class B > def initialize(x) > x.call > end > def display > puts 'goodbye' > end > end > > x = OpenProc { display } > > A.new(x) --> hello > B.new(x) --> goodbye > > in this way an object can, in effect, ask another object what it should > do. and that is what i was accomplishing, albiet in a lame way, by > passing self (and wanting self_parent for). > > so, is there any way to readily accomplish this in ruby? One way could be to use eval with a binding coming from the object, e.g.: class A def get_binding return binding end def display puts 'hello' end end a = A.new eval("display", a.get_binding) But eval is evil. :-) Another is what I think is called a `singleton method': a = A.new def a.do_this display end a.do_this However, it would be nice if Proc objects could refer not necessarily to the context where they were created, but to an arbitrary one. But I suspect this would pose many difficulties. Massimiliano