From: Mark Abramov on 10 Jun 2010 10:20 Mark Abramov wrote: > [tl;dr] Sorry, guys, didn't notice how I used eql instead of eql? Btw, without #hash it won't work anyways which I consider *weird* at the very least. -- Posted via http://www.ruby-forum.com/.
From: Rein Henrichs on 10 Jun 2010 11:27 On 2010-06-10 06:59:40 -0700, Robert Dober said: > On Thu, Jun 10, 2010 at 3:41 PM, Wilson Bilkovich <wilsonb(a)gmail.com> wrote: > Even if >> your code lives in isolation, ensuring proper semantics via these >> methods prevents a class of tricky bug that your successors may have >> to deal with. > Hmm? Would you care to show an example where overloading those methods > (#eql? and #hash) is needed to ensure proper behavior? I am willing to > learn. But I am not willing to accept this statement as such. > Cheers > R. You have been presented with one in this very thread. The OP wants objects of his class to have the correct semantics for Array#& and Hash#[], etc. The correct answer is to implement #hash and #eql?, just as implementing <=> provides objects of his class with the correct semantics for Array#sort. -- Rein Henrichs http://puppetlabs.com http://reinh.com
From: Rein Henrichs on 10 Jun 2010 11:28 On 2010-06-10 07:20:03 -0700, Mark Abramov said: > Mark Abramov wrote: >> [tl;dr] > > Sorry, guys, didn't notice how I used eql instead of eql? > Btw, without #hash it won't work anyways which I consider *weird* at the > very least. #hash makes sense for Hash#[] and etc. #eql? makes more sense for Array#&. I too find it odd that both are necessary. -- Rein Henrichs http://puppetlabs.com http://reinh.com
From: Marcin Wolski on 10 Jun 2010 12:00 Rein Henrichs wrote: > On 2010-06-10 07:20:03 -0700, Mark Abramov said: > >> Mark Abramov wrote: >>> [tl;dr] >> >> Sorry, guys, didn't notice how I used eql instead of eql? >> Btw, without #hash it won't work anyways which I consider *weird* at the >> very least. > > #hash makes sense for Hash#[] and etc. #eql? makes more sense for > Array#&. I too find it odd that both are necessary. If two objects are set to be eql?, their hash methods must also return the same value. More details in The Ruby Programming Language book. Thus, when you redefine eql?, the hash methods also should be redefined. -- Posted via http://www.ruby-forum.com/.
From: Mark Abramov on 10 Jun 2010 12:00
Marcin Wolski wrote: > Rein Henrichs wrote: >> On 2010-06-10 07:20:03 -0700, Mark Abramov said: >> >>> Mark Abramov wrote: >>>> [tl;dr] >>> >>> Sorry, guys, didn't notice how I used eql instead of eql? >>> Btw, without #hash it won't work anyways which I consider *weird* at the >>> very least. >> >> #hash makes sense for Hash#[] and etc. #eql? makes more sense for >> Array#&. I too find it odd that both are necessary. > > If two objects are set to be eql?, their hash methods must also return > the same value. More details in The Ruby Programming Language book. > > Thus, when you redefine eql?, the hash methods also should be redefined. http://ruby-doc.org/core-1.8.7/classes/Object.html#M000617 Well, it doesn't say much in core api :( -- Posted via http://www.ruby-forum.com/. |