A Scalar is the simplest object that can be evaluated. It holds a single value. When used in an Expression, this value must respond to the symbol (i.e. support the method) defined by the Expression#operator.
Scalar.new(1)A Variable represents a named value stored in the Environment. Unlike Scalars, Variables have no value until they are evaluated by an Environment. Evaluating a Variable that isn't present in the Environment will result in a MissingVariableError.
Variable.new("variable_a")An expression represents 2 or more operands that are reduced using a defined operator. The operands of an Expression can be Scalars, Variables, or other Expressions.
And Expression can store its result back into the Environment by defining an output.
# addition
Expression.new(:+, Scalar.new(1), Scalar.new(2))
# multiplication
Expression.new(:*, Variable.new("variable_a"), Scalar.new(2))
# storing output
Expression.new(:+, Scalar.new(1), Scalar.new(2), output: "one_plus_two")The Environment holds state in the form of a variables hash and can evaluate Expressions, Scalars, and Variables within a context. The environment handles updates to the state as Expressions run.
environment = Environment.new(
"variable_a" => 1,
"variable_b" => 2,
)
environment.evaluate(Variable.new("variable_a"))
#=> 1
environment.evaluate(
Expression.new(
:+,
Variable.new("variable_a"),
Variable.new("variable_b"),
output: "variable_c" # defines where to store the result value
)
)
#=> 3
environment.variables
#=> { "variable_a" => 1, "variable_b" => 2, "variable_c" => 3 }When evaluating multiple objects at a time, the value of the last object will be returned.
environment = Environment.new
environment.evaluate(
Scalar.new(1),
Expression.new(:+, Scalar.new(1), Scalar.new(2))
)
#=> 3All models support serialization via:
as_json: builds a serializable hash representation of the objectto_json: builds a JSON string representing the object
- Make sure you have Ruby installed, see
.ruby-versionfor the version. - Install Bundler:
gem install bundler - Use bundler to install dependencies:
bundle install
Spec files are located in /test. Run all tests using bundle exec rake test.