| Class | SymbolTable |
| In: |
lib/symboltable.rb
|
| Parent: | Hash |
# File lib/symboltable.rb, line 2 2: def initialize(hash=nil) 3: super() 4: merge!(hash) unless hash.nil? 5: end
Retrieves the value of the given key.
# File lib/symboltable.rb, line 18
18: def [](key)
19: super(key.to_sym) if valid_key?(key)
20: end
Returns true if this table contains the given key.
# 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.
# 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.
# 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.
# 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