Don't understand hashing

Hi,

Don’t know why this happens:

class Lazy
self.methods.reject{|m| m =~ /^(.*)$/}.each {|m|
eval “def #{m}(*args, &block) getobj.send(:’#{m}’, *args, &block) end”
}

def initialize(&block)
  @block = block
end

def __getobj__
  if @block
    @value = @block.call(*@args)
    @block = nil
  end
  return @value
end

def method_missing(id, *args, &block)
  __getobj__.send(id, *args, &block)
end

def hash
  __getobj__.hash
end

end

id1 = Lazy.new {:Id}
id2 = :Id

p id1.hash == id2.hash # => true

h1 = {}
h2 = {}
h1[id1] = "id1"
h2[id2] = “id2”

p h1[id2] # => nil (expect: “id1”)
p h2[id1] # => “id2”

I would have expected “id1” for h1[id2].
Can someone enlighten me?

Thanks.

Michael

  p id1.hash == id2.hash # => true

     p id2.eql?(id1)

  h1 = {}

Guy Decoux

“Michael Neumann” mneumann@ntecs.de schrieb im Newsbeitrag
news:20040501141807.GD794@miya.intranet.ntecs.de

Hi,

Don’t know why this happens:

class Lazy
self.methods.reject{|m| m =~ /^(.*)$/}.each {|m|
eval “def #{m}(*args, &block) getobj.send(:‘#{m}’, *args,
&block) end”
}

def initialize(&block)
  @block = block
end

def __getobj__
  if @block
    @value = @block.call(*@args)
    @block = nil
  end
  return @value
end

def method_missing(id, *args, &block)
  __getobj__.send(id, *args, &block)
end

def hash
  __getobj__.hash
end

end

id1 = Lazy.new {:Id}
id2 = :Id

p id1.hash == id2.hash # => true

h1 = {}
h2 = {}

Did you really mean to use two hashes?

robert
···

h1[id1] = “id1”
h2[id2] = “id2”

p h1[id2] # => nil (expect: “id1”)
p h2[id1] # => “id2”

I would have expected “id1” for h1[id2].
Can someone enlighten me?

Thanks.

Michael

To expand upon this, in a Hash identical keys have an identical hash
and are eql?. Keys that are not eql? have collided, and therefore
refer to different entries.

···

ts (decoux@moulon.inra.fr) wrote:

p id1.hash == id2.hash # => true

 p id2.eql?(id1)


Eric Hodel - drbrain@segment7.net - http://segment7.net
All messages signed with fingerprint:
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04