Hello Guys,
There is an interesting method provided by hash called 'rehash'. It rebuilds the hash based on the current hash values for each key. If values of key objects have changed since they were inserted, this method will reindex hash. If Hash.rehash is called while an iterator is traversing the hash, an IndexError will be raised in the iterator.
a = [ "p", "q" ]
b = [ "r", "s" ]
h = { a => 100, b => 200 }
h[a] returns 100
a[0] = "z"
h[a] returns nil. we have to rebuild the hash after updating key using rehash method of hash.
h.rehash returns {["z", "q"] => 100, ["r", "s"] => 200}
h[a] returns 100
There is an interesting method provided by hash called 'rehash'. It rebuilds the hash based on the current hash values for each key. If values of key objects have changed since they were inserted, this method will reindex hash. If Hash.rehash is called while an iterator is traversing the hash, an IndexError will be raised in the iterator.
a = [ "p", "q" ]
b = [ "r", "s" ]
h = { a => 100, b => 200 }
h[a] returns 100
a[0] = "z"
h[a] returns nil. we have to rebuild the hash after updating key using rehash method of hash.
h.rehash returns {["z", "q"] => 100, ["r", "s"] => 200}
h[a] returns 100