Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 516591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:44:49+00:00 2026-05-13T07:44:49+00:00

In Ruby, can one object destroy another? For example: class Creature def initialize @energy

  • 0

In Ruby, can one object destroy another?

For example:

class Creature
  def initialize
    @energy = 1
  end
  attr_accessor :energy
 end

class Crocodile < Creature
  def eat(creature)
    @energy += creature.energy
    creature = nil #this does not work
  end
end

fish = Creature.new
croc = Crocodile.new
croc.eat(fish)

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.

I know that if I say fish = nil, the object that the varible fish refers to will be garbage collected. But saying creature = nil inside the Crocodile’s eat method doesn’t accomplish that.

Another way of putting it

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?"

Update: problem solved

I’ve essentially taken the approach that Chuck suggested, with some modifications. Here was my reasoning:

  1. If there is no longer any variable pointing to an object, it will be garbage collected
  2. 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
  3. It seems logical that a list of all creatures should be stored in the Creature class

Therefore, I did this:

  1. On the Creature class object, I created a hash and assigned it to an instance variable. We’ll call it @creaturelist. (The reason I used an instance variable and not a class variable is so that any subclass of Creature can have its own list, too.)
  2. In the Initialize method, a new creature hands itself to the Creature class
  3. The Creature class adds a reference to that creature to @creaturelist and returns an ID to the creature.
  4. The creature remembers that ID in its own @id variable.
  5. If the creature dies, it calls the parent class with Creature.remove(@id), and the only reference to itself gets deleted.

Now I can do this:

class Predator < 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

Of course, in this example, fish 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T07:44:50+00:00Added an answer on May 13, 2026 at 7:44 am

    I think the problem is that you’re thinking of the program itself as the world in which these simulated things live rather than simulating one.

    fish = Creature.new
    croc = Crocodile.new
    $world = [fish, croc]
    class Crocodile
      def eat(creature)
        @energy += creature.energy
        $world.delete creature
      end
    end
    croc.eat fish
    world # [croc], now all by his lonesome, the last creature in the world :(
    

    And assuming the fish variable had gone out of scope like it would in a properly structured program, that object would most likely now be garbage.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 444k
  • Answers 444k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer With 600+ elements this is going to be dead slow.… May 15, 2026 at 6:39 pm
  • Editorial Team
    Editorial Team added an answer The first time you use this line: DateTimePicker.Value = System.DateTime.Now.AddDays(30);… May 15, 2026 at 6:39 pm
  • Editorial Team
    Editorial Team added an answer Not sure how you want to automate this, through script,… May 15, 2026 at 6:39 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.