Hi,

I just had to debug a fairly obscure problem with a colleague's code.

He was using a variable called id as an iterator variable and then using
it to index a hash he was populating. Somewhere in the code, it appeared
that id was mysteriously changing values and, for the life of me, I
couldn't figure out where or why this was happening. Even freezing the
variable didn't provide any clues.

After eliminating all other possible causes, I was left with the
conclusion that id must be a Ruby keyword or something like that. I
reached for my copy of 'Ruby in a Nutshell', consulted the index, and
sure enough, there's a method called Kernel#id, so everywhere in the
code that my colleague was setting what he thought was a locally scoped
variable, he was actually setting self.id.

My question is: since Kernel#id gives the internal Ruby identifier of
the object in question, something that does not change, why does this
method allow the setting of the id of the object? Is there a valid
reason to write to the id?

If this had been a read-only attribute, an exception would have been
thrown when he tried to set its value and debugging the issue would have
been a lot quicker. It seems to me that the id of an object should be
immutable.

Ian
-- 
Ian Macdonald               | Let no guilty man escape.   -- U.S. Grant 
System Administrator        | 
ian / caliban.org             | 
http://www.caliban.org      | 
                            |