Class: Keisan::Calculator
- Inherits:
-
Object
- Object
- Keisan::Calculator
- Defined in:
- lib/keisan/calculator.rb
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
Instance Method Summary collapse
- #define_function!(name, function) ⇒ Object
- #define_variable!(name, value) ⇒ Object
- #evaluate(expression, definitions = {}) ⇒ Object
-
#initialize(context = nil) ⇒ Calculator
constructor
A new instance of Calculator.
Constructor Details
#initialize(context = nil) ⇒ Calculator
Returns a new instance of Calculator.
5 6 7 |
# File 'lib/keisan/calculator.rb', line 5 def initialize(context = nil) @context = context || Context.new end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
3 4 5 |
# File 'lib/keisan/calculator.rb', line 3 def context @context end |
Instance Method Details
#define_function!(name, function) ⇒ Object
28 29 30 |
# File 'lib/keisan/calculator.rb', line 28 def define_function!(name, function) context.register_function!(name, function) end |
#define_variable!(name, value) ⇒ Object
24 25 26 |
# File 'lib/keisan/calculator.rb', line 24 def define_variable!(name, value) context.register_variable!(name, value) end |
#evaluate(expression, definitions = {}) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/keisan/calculator.rb', line 9 def evaluate(expression, definitions = {}) context.spawn_child do |local| definitions.each do |name, value| case value when Proc local.register_function!(name, value) else local.register_variable!(name, value) end end Keisan::AST::Builder.new(string: expression).ast.value(local) end end |