From: Matthew B Gardner Date: 2007-08-19T00:37:26+09:00 Subject: Re: Problem with Array#delete > I went a little different route, but that was indeed the problem. I just > defined == in my class, which I guess was comparing the class of the object > instead of the object_id. I did: > > def == obj > ������self.equal?(obj) > end Ok, so it looks like this didn't work so well after all. Initially, I thought it did because I reloaded the character.rb file while the program was running and it worked fine. However, when I shutdown the program and started it back up, I experienced the same problem (though it again worked after reloading the file). I'll post all the necessary code: def world returns the world object end class World def initialize characters = [] end end class Account def foo @character = Character.new() world.characters << @character end end class Character def quit p self.object_id p world.characters.class p world.characters.size world.characters.each do |ch| p ch.class p ch.object_id end world.characters.delete(self) p world.characters end end This is the output from Character#quit, with two character objects in the world#characters array: -607170236 Array 2 Character -607170236 Character -607240286 [] As you can see, both Character objects are being deleted when only the first should be (at least as far as my intention). In an attempt to fix this, I added the following method: class Character def == obj self.equal?(obj) end end I thought this did fix it, because I reloaded the file and tested it -- and it only deleted the targeted object. However, when I restarted the program, it didn't work and deleted both objects again. Without changing anything, and just reloading the file while the program was still running, it worked as intended again and deleted only the targeted object. I have an Account class that utilizes World#accounts (an Array object) in the exact same way that Character utilizes World#characters, and the deletion of accounts works fine. Robert, thank you for your reply -- I hope that this answers the noted questions that you had, and maybe even provides some more insight into what the problem might be. Is there an explanation for why the Character#== method works after being reloaded, but not at start time? Also, Robert, I reviewed the portion of your reply regarding a Struct object, and it doesn't look like it would be beneficial in my case -- I'm just trying to add and delete single instances of a class from an array -- but I could be wrong? Thanks again for any help, Matt