سؤال

I'm looking at a piece of code that suffers from self schizophrenia. One object is wrapping another object and to the programmer this is hidden and the code will expect the identity of the wrapper and the wrapped to be the same. This is only related to object_id and not to any method calls including comparions. I know that the VM would have problems if the wrapper would give of the same object_id as the wrapped but are there any Kernel, Class, Module methods (or other commonly used methods) that relies on the object_id to behave correctly?

In example

I might have code like

class HashSet
  def add(x)
     if @objects.has_key? x.object_id
       false
     else
       @objects[x.object_id] = x
     end 
  end
end

If I expect the call to add to return false I will be surprised that I can actuallly add the same object twice (I'm unaware of the wrapper).

To restate the question: are there any Kernel, Class, Module methods (or other commonly used methods) that relies on the object_id to behave correctly?

هل كانت مفيدة؟

المحلول

Hash instances have a compare_by_identity mode:

a1 = "a"
a2 = "a"
p a1.object_id == a2.object_id #=>false
h = {}
h.compare_by_identity
h[a1] = 0
h[a2] = 1

p h # => {"a"=>0, "a"=>1}
p h["a"] # => nil
p h[a2] # => 1
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top