From: Matthew B Gardner Date: 2007-08-19T09:58:55+09:00 Subject: Re: Problem with Array#delete Hello - On Saturday 18 August 2007 20:32, David A. Black wrote: > Hi -- > > On Sun, 19 Aug 2007, Matthew B Gardner wrote: > > Hello - > > > >> On Saturday 18 August 2007 17:56, David A. Black wrote: > >> Hi -- > >> > >> On Sun, 19 Aug 2007, Matthew B Gardner wrote: > >>> Thank you to everyone who has tried to help me with this today -- I > >>> know for certain -what- the problem is now, after adding in some > >>> testing. My Character class, for whatever reason, is using == to > >>> evaluate class. So, when I am trying to use Array#delete > >>> (array.delete(self) for a Character object), it's deleting every > >>> Character object in that array instead of just that single instance. > >>> I've tried to fix this issue by defining the == method in my Character > >>> class like this: > >>> > >>> def Character > >>> def == obj > >>> self.equal?(obj) > >>> end > >>> end > >>> > >>> However, this doesn't initially work -- I have to reload the file > >>> during runtime for it to utilize the above method. For sake of clarity, > >>> this is what I mean: > >>> > >>> Start program... > >>> a = Character.new > >>> b = Character.new > >>> a == b #=> true > >>> Reload character.rb file while program is running... > >>> a == b #=> false > >>> > >>> Hopefully that gets my meaning across. I'm not sure why Character#== > >>> isn't initially getting used, or if there's something wrong with my > >>> declaration. I'm also not sure why Character#== (prior to my defining > >>> Character#==) is comparing class and my other classes aren't. > >>> > >>> I hope that this description is satisfactory -- and thank you for any > >>> help that can be offered. > >> > >> I still can't tell what's happening without seeing actual code that's > >> doing what you're describing. Have you got a running example? > >> > >> > >> David > > > > Here's the pertinent code: > > > > require 'singleton' > > > > class Engine > > include Singleton > > attr_reader :world > > def initialize > > @world = nil > > end > > > > def startup > > @world = World.new > > end > > end > > > > class World > > attr_accessor :characters > > def initialize > > @characters = [] > > end > > end > > > > class Root > > def initialize name > > @name = name > > end > > end > > > > class Account > > attr_accessor :character > > def initialize > > @character = nil > > end > > def foo > > @character = Character.new "testname", self > > world.characters << @character > > end > > end > > > > class Mind < Root > > def initialize name > > super(name) > > end > > end > > > > class Character < Mind > > def initialize name, acc > > super(name) > > @account = acc > > end > > def quit > > world.characters.delete self > > end > > end > > > > def world > > Engine.instance.world > > end > > > > Engine.instance.startup > > acc1 = Account.new > > acc1.foo > > acc2 = Account.new > > acc2.foo > > p world.characters.size > > acc1.character.quit > > p world.characters.size > > > > > > However, this isn't reproducing my error (as you can see from the > > printout). As I've said before, I have other World#array variables that > > function the exact same way and don't run into the error I've been > > experiencing. Hopefully the code helps pinpoint (or at least hint at) why > > Character#== is evaluating class instead of identity, or why I have to > > reload the file for my defined Character#== to take effect. > > I'm feeling a little stupid, but where do you define Character#== ? > > > David I didn't include it with my code, but I wrote it in like this: class Character < Mind def initialize name, acc super(name) @account = acc end def == obj self.equal?(obj) end def quit world.characters.delete self end end This addition only works after I reload the character.rb file while the program is running. Initially (until I reload the file), world#characters.delete deletes all of the Character objects -- after the reload it only delete the instance. Does this help at all? Thanks again, Matt