When is a key not a key?

D

Dave Burt

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
 
R

Robert Klemme

Dave Burt said:
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
 
D

Dave Burt

Robert Klemme said:
- 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,161
Messages
2,570,891
Members
47,423
Latest member
henerygril

Latest Threads

Top