Class: ExpressionInterpreter::Expression
- Inherits:
-
Object
- Object
- ExpressionInterpreter::Expression
- Defined in:
- lib/expression_interpreter.rb
Instance Method Summary collapse
- #evaluate(&var) ⇒ Object
-
#initialize(expression) ⇒ Expression
constructor
A new instance of Expression.
- #to_s ⇒ Object
Constructor Details
#initialize(expression) ⇒ Expression
Returns a new instance of Expression.
4 5 6 7 8 |
# File 'lib/expression_interpreter.rb', line 4 def initialize(expression) parser = Parser.new(expression) @operation = parser.operation @variables = parser.variables end |
Instance Method Details
#evaluate(&var) ⇒ Object
10 11 12 13 14 15 16 |
# File 'lib/expression_interpreter.rb', line 10 def evaluate(&var) context = Context.new @variables.each do |variable| context.assign(variable, yield(variable.name)) end @operation.evaluate(context) end |
#to_s ⇒ Object
18 19 20 |
# File 'lib/expression_interpreter.rb', line 18 def to_s @operation.to_s end |