raffayatiq
raffayatiq

Reputation: 159

nil result despite both #hash and #eql? evaluating to true, when looking up a key in a hash?

I am wondering why this ruby hash evaluates to nil when I shovel into an array (which is the key for the hash) when the key is the same, according to #eql? and checking for the hashes equality.

some_arr = [1]
=> [1]
my_hash = { some_arr => "value" }
=> {[1]=>"value"}
my_hash[some_arr]
=> "value"
some_arr << 2
=> [1, 2]
my_hash[some_arr]
=> nil
my_hash
=> {[1, 2]=>"value"}

Both #eql? and checking for the hashes equality evaluate to true:

some_arr.hash == my_hash.keys[0].hash
=> true
some_arr.eql? my_hash.keys[0]
=> true

Not even using the array [1,2] gives the value:

my_hash[[1,2]]
=> nil

Upvotes: 2

Views: 103

Answers (1)

B-M
B-M

Reputation: 1308

In this case, your hash becomes outdated. Use rehash to solve your problem.

my_hash.rehash

Upvotes: 9

Related Questions