From: Niklas Frykholm Date: 2002-06-07T07:53:30+09:00 Subject: Re: Getting a reference to the object which called a method [Mathew Johnston]: > Hmm :) What I'm trying to do is ensure that only the object which > created another object can call it's methods. For example, object A > instantiates object B. In object B's initialize method, I would set an > object variable (@creator), referencing the object that called .new. On > method calls, I would "raise 'Method called by non-creator' if [insert > method to get ref to caller object].id == @creator.id" > > Is there another way to do this? I would probably do this by creating a proxy to the object with a safe interface. Something like this: class RealClass def initialize ... end def safe_method ... end def dangerous_method ... end end class SafeProxy def initialize(real_object) @real_object = real_object end def safe_method @real_object.safe_method end end The creator object a would create b with b = RealClass.new(...) It would keep this reference (which allows full access to b) to itself. When it wanted to give other objects a reference to b, it would give them SafeProxy.new(b) Which would ensure that the other classes could only access the safe methods in b. // Niklas