class SymbolTable

Public Class Methods

new(hash=nil) click to toggle source
Calls superclass method
# File lib/symboltable.rb, line 2
def initialize(hash=nil)
  super()
  merge!(hash) unless hash.nil?
end

Public Instance Methods

[](key) click to toggle source

Retrieves the value of the given key.

Calls superclass method
# File lib/symboltable.rb, line 18
def [](key)
  super(key.to_sym) if valid_key?(key)
end
[]=(key, val)
Alias for: store
key?(key) click to toggle source

Returns true if this table contains the given key.

Calls superclass method
# File lib/symboltable.rb, line 13
def key?(key)
  valid_key?(key) && super(key.to_sym)
end
merge!(other_hash) { |k, self, v| ... } click to toggle source

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
Also aliased as: update
method_missing(sym, *args) click to toggle source

Allows values to be retrieved and set using Ruby's dot method syntax.

Calls superclass method
# 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
store(key, val) click to toggle source

Sets the value of the given key to val.

Calls superclass method
# 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
Also aliased as: []=
to_hash() click to toggle source

Returns a Hash

# File lib/symboltable.rb, line 58
def to_hash
  Hash[self]
end
update(other_hash)
Alias for: merge!
valid_key?(key) click to toggle source

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