Class: Keisan::Context
- Inherits:
-
Object
- Object
- Keisan::Context
- Defined in:
- lib/keisan/context.rb
Instance Attribute Summary collapse
-
#function_registry ⇒ Object
readonly
Returns the value of attribute function_registry.
-
#variable_registry ⇒ Object
readonly
Returns the value of attribute variable_registry.
Instance Method Summary collapse
- #function(name) ⇒ Object
-
#initialize(parent: nil, random: nil) ⇒ Context
constructor
A new instance of Context.
- #random ⇒ Object
- #register_function!(name, function) ⇒ Object
- #register_variable!(name, value) ⇒ Object
- #spawn_child ⇒ Object
- #variable(name) ⇒ Object
Constructor Details
#initialize(parent: nil, random: nil) ⇒ Context
Returns a new instance of Context.
5 6 7 8 9 10 |
# File 'lib/keisan/context.rb', line 5 def initialize(parent: nil, random: nil) @parent = parent @function_registry = Functions::Registry.new(parent: @parent.try(:function_registry)) @variable_registry = Variables::Registry.new(parent: @parent.try(:variable_registry)) @random = random end |
Instance Attribute Details
#function_registry ⇒ Object (readonly)
Returns the value of attribute function_registry.
3 4 5 |
# File 'lib/keisan/context.rb', line 3 def function_registry @function_registry end |
#variable_registry ⇒ Object (readonly)
Returns the value of attribute variable_registry.
3 4 5 |
# File 'lib/keisan/context.rb', line 3 def variable_registry @variable_registry end |
Instance Method Details
#function(name) ⇒ Object
20 21 22 |
# File 'lib/keisan/context.rb', line 20 def function(name) @function_registry[name.to_s] end |
#random ⇒ Object
36 37 38 |
# File 'lib/keisan/context.rb', line 36 def random @random || @parent.try(:random) || Random.new end |
#register_function!(name, function) ⇒ Object
24 25 26 |
# File 'lib/keisan/context.rb', line 24 def register_function!(name, function) @function_registry.register!(name.to_s, function) end |
#register_variable!(name, value) ⇒ Object
32 33 34 |
# File 'lib/keisan/context.rb', line 32 def register_variable!(name, value) @variable_registry.register!(name.to_s, value) end |
#spawn_child ⇒ Object
12 13 14 15 16 17 18 |
# File 'lib/keisan/context.rb', line 12 def spawn_child if block_given? yield self.class.new(parent: self) else self.class.new(parent: self) end end |
#variable(name) ⇒ Object
28 29 30 |
# File 'lib/keisan/context.rb', line 28 def variable(name) @variable_registry[name.to_s] end |