Class: StaticHash

Inherits:
Hash show all
Defined in:
lib/mega/statichash.rb

Overview

:title: StaticHash

A Hash object which raises an error if any previously-defined key attempts to be set again.

Synopsis

foo = Hash::Static.new
foo['name'] = 'Tom'    #=> 'Tom'
foo['age']  = 30       #=> 30
foo['name'] = 'Bob'

produces

Error: StaticHash has value for key 'name' in object:
    {"name"=>"Tom", "age"=>30} (RuntimeError)

Author(s)

  • Gavin Kistner

Instance Method Summary collapse

Methods included from DupReplaceSnapshotMixin

#restore_snapshot, #take_snapshot

Instance Method Details

#[]=(key, val) ⇒ Object

Set a value for a key; raises an error if that key already exists with a different value.



51
52
53
54
55
56
# File 'lib/mega/statichash.rb', line 51

def []=(key,val)
  if self.has_key?(key) && self[key]!=val
    raise ArgumentError, "StaticHash already has value for key '#{key.to_s}'"
  end
  super
end