Sha256: ef5e1c1f9b97b51effd19d3736033c0c50d7387ebc9da781d465c28be4fd578c

Contents?: true

Size: 1.41 KB

Versions: 1

Compression:

Stored size: 1.41 KB

Contents

#--
# StaticHash
#
# Copyright (c) 2005 Thomas Sawyer
#
# from Gavin Kistner's WriteOnceHash in basiclibrary.rb
# Copyright (c) 2004 Gavin Kistner
#
# Reuse License
#
# It is covered under the license viewable at http://phrogz.net/JS/_ReuseLicense.txt
# Reuse or modification is free provided you abide by the terms of that license.
# (Including the first two lines above in your source code usually satisfies
# the conditions.)
#
# ==========================================================================
#  Revision History
# ==========================================================================
#
#  04.06.01 trans  Ported to Mega
#
# ==========================================================================
#++

#: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
#

class StaticHash < Hash

  # Set a value for a key;
  # raises an error if that key already exists with a different value.
  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

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mega-0.3.1 lib/mega/statichash.rb