Explicit destroy

Hi,
Is there a way to explicitly destroy an object ruby ?
Am I fool to want to do this ?? :wink:

I’m working on agents programation,
and I try to use Ruby for this,
but I’d like to have the possibility to kill an agent,
so other agents should have to perceive the death … but if they have a
reference on the killed agent, the object is still in ObjectSpace …

thanks

···


Ludo coquelle@enib.fr
ENIB/LI2

Ludo wrote:

Is there a way to explicitly destroy an object [in] Ruby?

No, you need to get rid of all references to that object and then let
the garbage collector destroy.

Am I fool to want to do this ?? :wink:

I wouldn’t use those words :wink: but generally speaking it’s dangerous to
destroy an object that other objects still hold references to. In C++
and other languages that use more explicit memory management these are
referred to as “dangling” pointers, and they’re the source of a lot of
debugging headaches.

I’m working on agents programation, and I try to use Ruby for this,
but I’d like to have the possibility to kill an agent,
so other agents should have to perceive the death … but if they have a
reference on the killed agent, the object is still in ObjectSpace …

OK. One solution that immediately comes to mind is some application of
the Observer pattern. When an agent is “killed”, part of its dying
process is to send a message out to any other interested agents:

 class Agent
   def die
     @friends.each do |friend|
       friend.notifyOfDeath(self)
     end
   end
 end

For this to work, we assume that the other agents (the “friends”) have
previously registered themselves with this agent, e.g.

 class Agent
   def befriend(anotherAgent)
     unless @friends.contains? anotherAgent
       @friends << anotherAgent
     end
   end
 end

You’ll also need to decide what should happen when an agent gets word of
another agent’s death, e.g.

 class Agent
   def notifyOfDeath(anAgent)
     # The agent "anAgent" is on his deathbed.
     # If I'm still holding any references to this agent
     # I need to get rid of them now!
   end
 end

And so a short example of its use would go like this:

 # Create some agents
 bond = Agent.new
 jinx = Agent.new

 # Jinx wants to know when Bond dies
 bond.befriend(jinx)

 # Kill Bond; this should trigger a call to
 # Jinx's notifyOfDeath() method.
 bond.die

Hope this helps,

Lyle

Ludo coquelle@enib.fr writes:

I’m working on agents programation,
and I try to use Ruby for this,
but I’d like to have the possibility to kill an agent,
so other agents should have to perceive the death … but if they have a
reference on the killed agent, the object is still in ObjectSpace …

If you want Jinx to be notified when Bond died, then it’s
do-able. http://www.rubygarden.org/ruby?GCAndMemoryManagement
section “Where’s the destructor” (near the end).

YS.

[snip]

And so a short example of its use would go like this:

 # Create some agents
 bond = Agent.new
 jinx = Agent.new

 # Jinx wants to know when Bond dies
 bond.befriend(jinx)

 # Kill Bond; this should trigger a call to
 # Jinx's notifyOfDeath() method.
 bond.die

I can see the movie now: Garbage Collect Another Day

Hal

···

----- Original Message -----
From: “Lyle Johnson” lyle@users.sourceforge.net
Newsgroups: comp.lang.ruby
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Wednesday, November 27, 2002 9:32 AM
Subject: Re: explicit destroy

sprintf(“%.3d”, ?\a) …

David

···

On Thu, 28 Nov 2002, Hal E. Fulton wrote:

----- Original Message -----
From: “Lyle Johnson” lyle@users.sourceforge.net
Newsgroups: comp.lang.ruby
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Wednesday, November 27, 2002 9:32 AM
Subject: Re: explicit destroy

[snip]

And so a short example of its use would go like this:

 # Create some agents
 bond = Agent.new
 jinx = Agent.new

 # Jinx wants to know when Bond dies
 bond.befriend(jinx)

 # Kill Bond; this should trigger a call to
 # Jinx's notifyOfDeath() method.
 bond.die

I can see the movie now: Garbage Collect Another Day


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

dblack@candle.superlink.net wrote:

sprintf(“%.3d”, ?\a) …

I am so lazy that I just fired up IRB and typed this in instead of
trying to figure out what it would do beforehand. But I’m glad you
caught the gratuitous references to 007 :wink: Hopefully I’ll get to the
theater over the Thanksgiving holiday to see the new movie.