# File lib/symboltable.rb, line 2 def initialize(hash=nil) super() merge!(hash) unless hash.nil? end
Retrieves the value of the given key.
# File lib/symboltable.rb, line 18 def [](key) super(key.to_sym) if valid_key?(key) end
Returns true if this table contains the given
key.
# File lib/symboltable.rb, line 13 def key?(key) valid_key?(key) && super(key.to_sym) end
Merges the values from other_hash into this table.
# File lib/symboltable.rb, line 33 def merge!(other_hash) raise ArgumentError unless Hash === other_hash other_hash.each do |k, v| if block_given? && key?(k) self[k] = yield(k, self[k], v) else self[k] = v end end end
Allows values to be retrieved and set using Ruby's dot method syntax.
# File lib/symboltable.rb, line 47 def method_missing(sym, *args) if sym.to_s =~ /(.+)=$/ self[$1] = args[0] elsif args.empty? self[sym] else super(sym, *args) end end
Sets the value of the given key to val.
# File lib/symboltable.rb, line 23 def store(key, val) if valid_key?(key) val = self.class.new(val) if Hash === val && !(SymbolTable === val) super(key.to_sym, val) end end
Returns a Hash
# File lib/symboltable.rb, line 58 def to_hash Hash[self] end
Returns true if the given key may be used in this table.
# File lib/symboltable.rb, line 8 def valid_key?(key) key.respond_to?(:to_sym) end