From: Robert Klemme Date: 2004-11-10T17:33:31+09:00 Subject: Re: When is a key not a key? "Dave Burt" schrieb im Newsbeitrag news:D7kkd.31098$K7.23161@news-server.bigpond.net.au... > Hi, > > I hesitate to write because some of my code here is pretty evil, but this > problem is weirding me out a bit. > > I have a hash (loan_shark) indexed by instances of a particular struct > (Player). > > The hash seems to be failing to find its keys when I try to look them up. > Like this: > > loan_shark.size #=> 4 > loan_shark.has_key?(loan_shark.keys[0]) #=> false > > Any ideas? Two possible reasons come to mind: - keys are modified after they were inserted into the hash but you did not rehash - some of the instances that you put into the Player struct do not implement #eql? and #hash appropriately. Did you verify that newb.eql?( newb ) is true? Solutions to both could be to implement #eql? and #hash on your own, for example if only "name" is the key. From what I read I feel that :hands does change over time and thus is maybe not suited to be included in the key or the key's #hash and #eql? calculation. Kind regards robert > > The background is something like this: > > Player = Struct.new(:name, :bankroll, :hands) > ... > loan_shark = {} > ... > newb = Player.new("Fred", some_dollars) > ... > loan_shark[newb] = some_dollars > ... > players << newb > > If you feel the need, you can get all the code (about 15k) here: > http://www.dave.burt.id.au/ruby/blackjack.rb > http://www.dave.burt.id.au/ruby/cards.rb > (The issue can be found near blackjack.rb:304, complete with futile > debugging code) > > Cheers, > Dave > >