Note that there are some explanatory texts on larger screens.

plurals
  1. POCan one Ruby object destroy another?
    primarykey
    data
    text
    <p>In Ruby, can one object destroy another?</p> <p>For example:</p> <pre><code>class Creature def initialize @energy = 1 end attr_accessor :energy end class Crocodile &lt; Creature def eat(creature) @energy += creature.energy creature = nil #this does not work end end fish = Creature.new croc = Crocodile.new croc.eat(fish) </code></pre> <p>After the crocodile has eaten a creature and absorbed its energy, the creature should cease to exist. But the code above doesn't destroy the creature.</p> <p>I know that if I say <code>fish = nil</code>, the object that the varible <code>fish</code> refers to will be garbage collected. But saying <code>creature = nil</code> inside the Crocodile's <code>eat</code> method doesn't accomplish that.</p> <h2>Another way of putting it</h2> <p>From inside croc.eat, can I say "since the variable 'fish' was passed to me, when I'm done, I'm going to set 'fish' to nil?"</p> <h2>Update: problem solved</h2> <p>I've essentially taken the approach that Chuck suggested, with some modifications. Here was my reasoning:</p> <ol> <li>If there is no longer any variable pointing to an object, it will be garbage collected</li> <li>If, when an object is created, I add it to a hash (like 'x' => object), and don't create any other variable for it, then deleting that item from the hash results in garbage collecting the object</li> <li>It seems logical that a list of all creatures should be stored in the Creature class</li> </ol> <p>Therefore, I did this:</p> <ol> <li>On the Creature class object, I created a hash and assigned it to an instance variable. We'll call it <code>@creaturelist</code>. (The reason I used an instance variable and not a class variable is so that any subclass of <code>Creature</code> can have its own list, too.)</li> <li>In the Initialize method, a new creature hands itself to the Creature class</li> <li>The Creature class adds a reference to that creature to <code>@creaturelist</code> and returns an ID to the creature.</li> <li>The creature remembers that ID in its own <code>@id</code> variable.</li> <li>If the creature dies, it calls the parent class with <code>Creature.remove(@id)</code>, and the only reference to itself gets deleted.</li> </ol> <p>Now I can do this:</p> <pre><code>class Predator &lt; Creature def eat(creature) @energy += creature.energy creature.die end end fish = Creature.new Creature.list #shows the fish croc = Predator.new croc.eat(fish) Creature.list #no more fish </code></pre> <p>Of course, in this example, <code>fish</code> still points to that creature object, so it's not garbage collected. But eventually, creatures will be created and eat each other based on rules, so I won't be individually naming them.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload