Class: Kalc::Environment
- Inherits:
-
Object
- Object
- Kalc::Environment
- Defined in:
- lib/kalc/environment.rb
Instance Attribute Summary collapse
-
#functions ⇒ Object
readonly
Returns the value of attribute functions.
-
#variables ⇒ Object
readonly
Returns the value of attribute variables.
Instance Method Summary collapse
- #add_function(name, value) ⇒ Object
- #add_variable(name, value) ⇒ Object
- #get_function(name) ⇒ Object
- #get_variable(name) ⇒ Object
-
#initialize(parent = nil) {|_self| ... } ⇒ Environment
constructor
A new instance of Environment.
Constructor Details
#initialize(parent = nil) {|_self| ... } ⇒ Environment
Returns a new instance of Environment.
6 7 8 9 10 11 |
# File 'lib/kalc/environment.rb', line 6 def initialize(parent = nil) @functions = {} @variables = {} @parent = parent yield self if block_given? end |
Instance Attribute Details
#functions ⇒ Object (readonly)
Returns the value of attribute functions.
3 4 5 |
# File 'lib/kalc/environment.rb', line 3 def functions @functions end |
#variables ⇒ Object (readonly)
Returns the value of attribute variables.
4 5 6 |
# File 'lib/kalc/environment.rb', line 4 def variables @variables end |
Instance Method Details
#add_function(name, value) ⇒ Object
13 14 15 |
# File 'lib/kalc/environment.rb', line 13 def add_function(name, value) @functions.update({ name.to_s.strip => value }) end |
#add_variable(name, value) ⇒ Object
27 28 29 30 |
# File 'lib/kalc/environment.rb', line 27 def add_variable(name, value) @variables.update({ name.to_s.strip => value }) value end |
#get_function(name) ⇒ Object
17 18 19 20 21 22 23 24 25 |
# File 'lib/kalc/environment.rb', line 17 def get_function(name) if fun = @functions[name.to_s.strip] fun elsif !@parent.nil? @parent.get_function(name) else nil end end |
#get_variable(name) ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'lib/kalc/environment.rb', line 32 def get_variable(name) if var = @variables[name.to_s.strip] var elsif !@parent.nil? @parent.get_variable(name) else nil end end |