Wednesday, 11 September 2013

Selfschizofrenia in Ruby

Selfschizofrenia in Ruby

I'm lloking 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
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).

No comments:

Post a Comment