Michael Neumann
12/13/2004 5:03:00 PM
Hi,
My observation is, that Hash behaves differently for different kinds of
objects. While an Object key is preserved as is, a String key isn't
(it's frozen and a copy is returned)! I'd like to get back my original
key from a Hash, but with String keys this is not possible.
a = 'test'
b = Object.new
h = {}
h[a] = a
h[b] = b
h.each do |k,v|
puts "key: "
p k
p k.object_id
p k.frozen?
puts "value: "
p v
p v.object_id
p v.frozen?
end
The result is (indented for readability):
key:
#<Object:0x81f239c>
68129230
false
value:
#<Object:0x81f239c>
68129230
false
key:
"test"
68135310
true
value:
"test"
68135320
false
Is this intended behaviour?
So in my case, instead of { obj => data }, I now have to store
{ obj => [obj, data] }, as I can't get the original object "obj" back.
Regards,
Michael