From: Robert Klemme on
On 06/10/2010 05:27 PM, Rein Henrichs wrote:
> 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.

See also
http://blog.rubybestpractices.com/posts/rklemme/018-Complete_Class.html
http://blog.rubybestpractices.com/posts/rklemme/019-Complete_Numeric_Class.html

Cheers

robert

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
From: Robert Dober on
On Thu, Jun 10, 2010 at 5:30 PM, Rein Henrichs <reinh(a)reinh.com> wrote:
> On 2010-06-10 06:59:40 -0700, Robert Dober said:
> 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.
I guess you really do not know what I was talking about? Or do you
just repeat the same stuff over and over again in order to convince
me?
overwriting #hash and #eql? breaks Hash! Why the hack should OP's
usecase justify this?
And it does not answer my question. Where would I like that Hash
behaves accordingly to the redefined #eql? and #hash. And BTW I asked
Wilson, did I not?
Cheers
Robert



--
The best way to predict the future is to invent it.
-- Alan Kay

From: Robert Dober on
On Thu, Jun 10, 2010 at 6:10 PM, Robert Klemme
<shortcutter(a)googlemail.com> wrote:

> http://blog.rubybestpractices.com/posts/rklemme/018-Complete_Class.html
> http://blog.rubybestpractices.com/posts/rklemme/019-Complete_Numeric_Class.html
I
You define #eql? and #hash for your convenience. So good, so bad. My
question simply was: Show my why *not* redefining #hash and #eql? will
cause problems, because that was Wilson's statement. I am still
waiting :(.

Cheers
R.


--
The best way to predict the future is to invent it.
-- Alan Kay

From: Mark Abramov on
Robert Dober wrote:
> On Thu, Jun 10, 2010 at 5:30 PM, Rein Henrichs <reinh(a)reinh.com> wrote:
> overwriting #hash and #eql? breaks Hash!
That's not true, I think.
--
Posted via http://www.ruby-forum.com/.

From: Robert Dober on
On Thu, Jun 10, 2010 at 6:48 PM, Mark Abramov <markizko(a)gmail.com> wrote:
> Robert Dober wrote:
>> On Thu, Jun 10, 2010 at 5:30 PM, Rein Henrichs <reinh(a)reinh.com> wrote:
>> overwriting #hash and #eql? breaks  Hash!
> That's not true, I think.
Judge for yourself

require "forwardable"

def count klass
ObjectSpace.each_object( klass ).to_a.size
end
class N
extend Forwardable
attr_reader :n
def_delegators :n, :hash
def eql? otha
n == otha.n
end
private
def initialize n
@n = n
end
end # class N


h = { N.new( 42 ) => true }
h[ N.new( 42 ) ] = 42
p h
GC.start
p count(N)

Cheers
R.

First  |  Prev  |  Next  |  Last
Pages: 1 2 3 4 5 6 7 8 9
Prev: [ANN] Money 3.0.2
Next: escape character with decimal value