0

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?

Rune FS
  • 21,497
  • 7
  • 62
  • 96
  • What is your actual question? None of the sentences in your post end in a question mark (apart from `has_key?`). – Raphael Schweikert Sep 11 '13 at 11:23
  • @RaphaelSchweikert "are there any Kernel, Class, Module methods (or other commonly used methods) that relies on the object_id to behave correctly." is missing the upper part of the question mark :) – Rune FS Sep 11 '13 at 11:36
  • 2
    I also don't see the problem here. What are you actually trying to achieve? In the code snippet above, if you are passing the same x object, it should return false. But still, where does @objects get initialised? – daLizard Sep 11 '13 at 11:36
  • 2
    And what do you mean by "object_id behaves correctly"? – daLizard Sep 11 '13 at 11:38
  • By “behave correcly”, do you mean: calling `object_id` yields different results for the wrapper and the wrapped object? – Raphael Schweikert Sep 11 '13 at 11:39
  • I'm not passing the same object due to self schizophrenia I only believe I am. It doesn't really matter for the example where @objects is initialized or of what type for that matter. The point is that the code relies on object_id. The code above is not my problem but an example of how it could manifest (Ie I don't need to fix the imaginary HashSet class) – Rune FS Sep 11 '13 at 11:39
  • @daLizard I said "relies on object_id to behave correctly" if object_id didn't behave correctly neither would the VM to my knowledge. What I mean is that the code above uses object_id and in the case of self schizofrenia this would be a problem. I'm looking for such problems in methods of E.g. Kernel, Class or Module – Rune FS Sep 11 '13 at 11:42
  • @RaphaelSchweikert the answer by steenslag is an example of what I'm looking for. Setting compare_by_identity on a hash would create problems if your code suffers from self schizophrenia issues – Rune FS Sep 11 '13 at 11:47

1 Answers1

2

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
steenslag
  • 79,051
  • 16
  • 138
  • 171