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?
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
Robert
(Robert)
10 November 2004 08:33
2
"Dave Burt" <dave@burt.id.au> 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
"Robert Klemme" <bob.news@gmx.net> wrote...
- keys are modified after they were inserted into the hash but you did
not rehash
Of course, that makes perfect sense.
I'll try and make my hash keys invariant to keep it simple.
Many thanks,
Dave
PS: Sorry about the bad links, they've been fixed now the problem's gone, so
you can see my evil but working code.