irb(main):005:0> v.hash
I’d have expected h[v2] => “whatever is in (4,5)”, since v==v2 and
both have the same hash value… what am I doing wrong?
It’s not enough that they have the same hash value or that obj1 ==
obj2. For the purposes of Hash, two objects are the same if
obj1.eql?(obj2). Does v1.eql?(v2)?
Testing reveals that they don’t. but that strikes me as a bug in the
Vector class. It violates the guidelines given in the PickAxe book
(bottom of page 568), and more importantly, it violates common
intuition. If two vectors that are created in exactly the same manner
aren’t eql?, they’re useless for use as hash keys. I’d say file a bug
report, or at least a problem, on this one.
=> 17
have the same hash value… what am I doing wrong?
It’s not enough that they have the same hash value or that obj1 == obj2.
For the purposes of Hash, two objects are the same if obj1.eql?(obj2).
Does v1.eql?(v2)?
identical) then it would work:
I think it is relevant, given that the Vector class is designed to be
immutable. I would agree that the Vector class should probably define #eql? as an alias for #==.
I think it is relevant, given that the Vector class is designed to be
immutable. I would agree that the Vector class should probably define #eql? as an alias for #==.
It appears to be a typo in Matrix.rb - This is in ruby1.8.4:
class Vector
…
Returns +true+ iff the two vectors have the same elements in the
same order.
def ==(other)
return false unless Vector === other
other.compare_by(@elements)
end
alias eqn? ==
~~~
And Vector seems to be the only implementor of eqn?
rick@frodo:~/ruby-1.8.4$ ri eqn
------------------------------------------------------------ Vector#eqn?
eqn?(other)
Alias for #==
And it’s not yet fixed in 1.9
rick@frodo:~/ruby-1.8.4$ grep eqn
/public/rubysource/ruby1.9/ruby/lib/matrix.rb
alias eqn? ==