Class SymbolTable
In: lib/symboltable.rb
Parent: Hash

Methods

[]   []=   key?   merge!   method_missing   new   store   to_hash   update   valid_key?  

Public Class methods

[Source]

   # File lib/symboltable.rb, line 2
2:   def initialize(hash=nil)
3:     super()
4:     merge!(hash) unless hash.nil?
5:   end

Public Instance methods

Retrieves the value of the given key.

[Source]

    # File lib/symboltable.rb, line 18
18:   def [](key)
19:     super(key.to_sym) if valid_key?(key)
20:   end
[]=(key, val)

Alias for store

Returns true if this table contains the given key.

[Source]

    # File lib/symboltable.rb, line 13
13:   def key?(key)
14:     valid_key?(key) && super(key.to_sym)
15:   end

Merges the values from other_hash into this table.

[Source]

    # File lib/symboltable.rb, line 33
33:   def merge!(other_hash)
34:     raise ArgumentError unless Hash === other_hash
35:     other_hash.each do |k, v|
36:       if block_given? && key?(k)
37:         self[k] = yield(k, self[k], v)
38:       else
39:         self[k] = v
40:       end
41:     end
42:   end

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

[Source]

    # File lib/symboltable.rb, line 47
47:   def method_missing(sym, *args)
48:     if sym.to_s =~ /(.+)=$/
49:       self[$1] = args[0]
50:     elsif args.empty?
51:       self[sym]
52:     else
53:       super(sym, *args)
54:     end
55:   end

Sets the value of the given key to val.

[Source]

    # File lib/symboltable.rb, line 23
23:   def store(key, val)
24:     if valid_key?(key)
25:       val = self.class.new(val) if Hash === val && !(SymbolTable === val)
26:       super(key.to_sym, val)
27:     end
28:   end

Returns a Hash

[Source]

    # File lib/symboltable.rb, line 58
58:   def to_hash
59:     Hash[self]
60:   end
update(other_hash)

Alias for merge!

Returns true if the given key may be used in this table.

[Source]

    # File lib/symboltable.rb, line 8
 8:   def valid_key?(key)
 9:     key.respond_to?(:to_sym)
10:   end

[Validate]