In message “Re: Bug in Hash#hash? (Ruby 1.8.4)”
on Wed, 20 Sep 2006 23:40:11 +0900, “Eyal O.” [email protected] writes:
|say one = {:a=>:b}
|then one.hash != one.clone.hash
|is that a bug or correct?
|
|(using ruby 1.8.4 (2005-12-24) [i486-linux])
Currently it’s an intended behavior. Hash is not designed to be hash
key (two hashes are considered to be a same key if they are exactly
the same object). It’s not likely to change in 1.8 series. In 1.9,
maybe.
Currently it’s an intended behavior. Hash is not designed to be hash
key (two hashes are considered to be a same key if they are exactly
the same object). It’s not likely to change in 1.8 series. In 1.9,
maybe.
And I think more precisely, it’s that hash needs to be implemented so
that a.eql?(b) implies a.hash == b.hash
one = {:a => :b}
two = one.clone
one == two #=> true
one.eql?(two) #=> false
Smalltalkers might recall that Smalltalk has both a Hash which uses
Smalltalks equivalents to Ruby’s hash and ==, and IdentityHash which
uses Smalltalks equivalents to Ruby’s object_id for the hash, and
equal?
|say one = {:a=>:b}
|then one.hash != one.clone.hash
|is that a bug or correct?
Currently it’s an intended behavior. Hash is not designed to be hash
key (two hashes are considered to be a same key if they are exactly
the same object). It’s not likely to change in 1.8 series. In 1.9,
maybe.
thanks for the clarification.
why was this decided?
is it not useful to have a hash as hash-key? I often use hashes to
emulate named parameter-values, and if I have say a connection pool I
would like to index it based on the connection-parameters (which are
given as a hash of key-value pairs).
In message “Re: Bug in Hash#hash? (Ruby 1.8.4)”
on Thu, 21 Sep 2006 09:34:17 +0900, “Rick DeNatale” [email protected] writes:
|Smalltalkers might recall that Smalltalk has both a Hash which uses
|Smalltalks equivalents to Ruby’s hash and ==, and IdentityHash which
|uses Smalltalks equivalents to Ruby’s object_id for the hash, and
|equal?
Recently, 1.9 added Hash#compare_by_identity method which makes Hash
to compare its keys by identity.
|say one = {:a=>:b}
|then one.hash != one.clone.hash
|is that a bug or correct?
Currently it’s an intended behavior. Hash is not designed to be hash
key (two hashes are considered to be a same key if they are exactly
the same object).
is it not useful to have a hash as hash-key? I often use hashes to
emulate named parameter-values, and if I have say a connection pool I
would like to index it based on the connection-parameters (which are
given as a hash of key-value pairs).
Not entirely to the point, but…
One issue you have to be aware of if you have a Hash with keys that
are mutable, is that if a key mutates so as to affect the results of
eql?/hash then the hash needs to be rehashed or strange events can
ensue.