Andres Colon wrote:
> In regards to this post, a more relevant example of an instance variable 
> reference problem is the following:
> 
> In a Game Object, there is an Area object.
> An area object has Player A and Player B, which are themselves objects.
> 
> When Player A interacts (such as attacks) Player B, both objects store a 
> reference to each other in order to interact.
> 
> So in this sense, the Game Object, Area Object and both players have 
> reference to player objects.
> 
> Now, if Player A quits the game, the Game Object might make the 
> reference to the Player A = nil.
> 
> It would be very helpful to be able to destroy the object, without 
> having to find all objects referencing this object to finally get Player 
> A to become nil.
> 
> I would appreciate help on this matter, on how to make a single object 
> nil and have that propagate to everything referencing it.

Two approaches come to mind. The first is make the Game Object 
observable (see observable.rb). When it is destroyed it notifies the 
objects that refer to it.

The second is to pass around an indirect reference to the Game Object. 
For example, make an array that contains only the Game Objecct and give 
the players a reference to the array. When the Game Object is destroyed 
it sets the array element to nil. The players never refer to the game 
object directly, only via the array and only after making sure it hasn't 
become nil.

-- 
RMagick: http://rmagick.rubyforge.org/