Hi Bulat,
When I first read your code below, it was also obvious to me, since the
original reference to the array [s] in the second line was lost
forever. In the last line, ["mutable"] is really a newly created object,
which is obviously different from the original array object.
But then, toying with your idea, I found this:
# ruby 1.6.7 (2002-03-01) [i686-linux]
s = "mutable"
arr = [s]
hash = { arr => "object" }
s.upcase!
p hash[arr] #=> nil
This is really surprising to me! Is this the expected behavior in
Ruby? Because in Python, using an array as a hash key is forbidden in the
first place (syntax error, I guess), while Ruby simply allows us to do so.
Any idea, anyone? (I really thought before that the hash key were
actually just the object ID in Ruby.)
Regards,
Bill
============================================================================
Bulat Ziganshin <bulatz / integ.ru> wrote:
> eklmn! add one level of indirection:
> s = "mutable"
> hash = { [s] => "object" }
> s.upcase!
> p hash[["mutable"]] #=> nil
> it's a well-known theoretical problem. call/asign by reference or by
> value, and mutable objects versus non-modified