Just Another Victim of the Ambient Morality
9/13/2006 3:43:00 PM
"Tim Pease" <tim.pease@gmail.com> wrote in message
news:69f66b790609130825u2a1df39bgff8899f3b881a1a1@mail.gmail.com...
> On 9/13/06, Just Another Victim of the Ambient Morality
> <ihatespam@rogers.com> wrote:
>> Suppose I want to define a class and be able to use this class as a
>> key
>> to a hash. How does one do this? ...And please test your response
>> before
>> posting it.
>
> Chapter 22 of the Programming Ruby, The Pragmatic Programmer's Guide
>
> A key must respond to "hash" and the value returned must not change.
>
> What is not in the book is that you must also provide an eql? method
> that evaluates to true when two objects are equivalent.
Yes! I found that suspiciously missing from the book, as well. After
a bit of thought, you figure out that some equality method must be
implemented as well as the hash method but I didn't know if it were ==,
eql?, <=>, or whatever...
Thanks a lot!
>> Secondly, if an objects works in a hash, is it guaranteed to work in
>> a
>> set?
>
> No idea about this one. Have not used the Ruby Set class thus far.
That's too bad. One would hope it will have exactly the same
requirements as for hash but who knows?