Class: Kalc::Environment

Inherits:
Object
  • Object
show all
Defined in:
lib/kalc/environment.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent = nil) {|_self| ... } ⇒ Environment

Returns a new instance of Environment.

Yields:

  • (_self)

Yield Parameters:



7
8
9
10
11
12
# File 'lib/kalc/environment.rb', line 7

def initialize(parent = nil)
  @functions = {}
  @variables = {}
  @parent = parent
  yield self if block_given?
end

Instance Attribute Details

#functionsObject (readonly)

Returns the value of attribute functions.



4
5
6
# File 'lib/kalc/environment.rb', line 4

def functions
  @functions
end

#variablesObject (readonly)

Returns the value of attribute variables.



5
6
7
# File 'lib/kalc/environment.rb', line 5

def variables
  @variables
end

Instance Method Details

#add_function(name, value) ⇒ Object



14
15
16
# File 'lib/kalc/environment.rb', line 14

def add_function(name, value)
  @functions.update({ name.to_s.strip => value })
end

#add_variable(name, value) ⇒ Object



28
29
30
31
# File 'lib/kalc/environment.rb', line 28

def add_variable(name, value)
  @variables.update({ name.to_s.strip => value })
  value
end

#get_function(name) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/kalc/environment.rb', line 18

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



33
34
35
36
37
38
39
40
41
# File 'lib/kalc/environment.rb', line 33

def get_variable(name)
  if var = @variables[name.to_s.strip]
    var
  elsif !@parent.nil?
    @parent.get_variable(name)
  else
    nil
  end
end