Hi Josh,

Here is how Hash in Ruby works when it tries to determine if two keys are e=
qual:
* the #hash method on both objects are called to calculate their hash codes
* if their hash codes are not equal, they are not equal
* if their hash codes are equal, then #=3D=3D is called to determine if
two objects are equal

In your example, all three objects actually return the same hash
codes, so #=3D=3D (instead of eql?) is used to check their equality.

The "first Josh" and the "second Josh" are equal because their #=3D=3D
(inherited from Object#=3D=3D) simply calls #eql? which you have
overridden to make them equal.

The "first Josh" is not equal to "Josh" because they are of different
classes, and User#=3D=3D (inherited from Object#=3D=3D) does not allow obje=
cts
of different classes to be equal.

As a side note: you should always define #hash and #=3D=3D together and
make sure whenever #=3D=3D returns true #hash mush return the same number,
otherwise, using these objects as hash keys will break the hash
semantics. Also, avoid using mutable objects as hash keys unless their
#hash number is immutable.

I hope this helps


On Fri, Dec 30, 2011 at 1:37 PM, Josh Cheek <josh.cheek / gmail.com> wrote:
> Hi, was playing around with an idea after reading the thread about defini=
ng
> #hash. My understanding was that #hash gives a unique identifier, and tha=
t
> #eql? allows the hash to determine whether the two objects are equal in
> terms of being the same hash key. So I wrote some code that should take a=
n
> equivalent instance, or a string for quick access. But it behaves in a wa=
y
> that I completely don't understand. Hoping someone can help:
>
>
> User =3D Struct.new :name, :age, :identifier do
> =A0def hash
> =A0 =A0name.hash
> =A0end
>
> =A0def eql?(other)
> =A0 =A0puts "#{name} was asked if they were equal to #{other.inspect}"
> =A0 =A0(other =3D=3D name) || (other.name =3D=3D name && other.age =3D=3D=
 age)
> =A0end
> end
>
> josh =3D User.new 'Josh', 28, 'first Josh'
> hash =3D {josh =3D> josh}
>
> hash[josh] =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0# =3D> #<struct User name=3D"Josh",
> age=3D28, identifier=3D"first Josh">
> hash[User.new 'Josh', 28, 'second Josh'] =A0# =3D> #<struct User name=3D"=
Josh",
> age=3D28, identifier=3D"first Josh">
> hash['Josh'] =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0#=
 =3D> nil
>
> # >> Josh was asked if they were equal to #<struct User name=3D"Josh",
> age=3D28, identifier=3D"first Josh">
>
>
>
> So I would have expected all three to go through eql? Instead, we see tha=
t
> only the case where the key was the same object goes through. However, it
> identifies that the second Josh is the same key, without invoking User#eq=
l?
> How does it do this?
>
> And why does the string "Josh" not find the instance?
>
> This is all probably in my copy of the Pickaxe, but it's in Chicago and I=
'm
> out of town :/