I'm looking for a kind of "append-only" hash where keys may only be set once.
For example:
capitals = AppendOnlyHash.new
capitals['france'] = 'paris'
capitals['japan'] = 'tokyo'
capitals['france'] = 'nice' # raises immutable exception
Any library recommendations or ideas how to achieve this?
(Use case is a logging type object which will be passed to numerouis loosely connected classes, and wanting to detect if any use the same key.)
There are 10 methods, directly mutating the hash:
Also, there is a possibility to mutate values themselves (
capitals['france'] << ' and Lyon',) so we are to prevent this as well.One needs to derive from
Hashbecause otherwise we are to break all the hashes.I did not test this code but it should work out of the box, (if not, the idea should be clear.)