Class: Hashery::StaticHash
- Defined in:
- lib/hashery/static_hash.rb
Overview
TODO:
Maybe StaticHash isn’t bets name for this class?
StaticHash ia a Hash object which raises an error if any previously-defined key attempts to be set again.
foo = StaticHash.new
foo['name'] = 'Tom' #=> 'Tom'
foo['age'] = 30 #=> 30
foo['name'] = 'Bob'
produces
ArgumentError: Duplicate key for StaticHash -- 'name'
StaticHash has it’s orgins in Gavin Kistner’s WriteOnceHash class found in his basiclibrary.rb
script.
Constant Summary
Constants inherited from CRUDHash
Instance Method Summary collapse
-
#store(key, value) ⇒ Object
Set a value for a key.
Methods inherited from CRUDHash
#<<, #[], [], #[]=, auto, #cast, #cast_key, #default_proc, #delete, #each, #fetch, #key?, #key_proc, #key_proc=, #merge, #read, #replace, #retrieve, #to_hash, #update, #values_at
Methods inherited from Hash
create, #rekey, #rekey!, #retrieve, #to_hash, #to_stash
Instance Method Details
#store(key, value) ⇒ Object
Set a value for a key. Raises an error if that key already exists with a different value.
31 32 33 34 35 36 |
# File 'lib/hashery/static_hash.rb', line 31 def store(key, value) if key?(key) && fetch(key) != value raise ArgumentError, "Duplicate key for StaticHash -- #{key.inspect}" end super(key, value) end |