Class: Boxcars::RubyCalculator
- Defined in:
- lib/boxcars/boxcar/ruby_calculator.rb
Overview
A Boxcar executes ruby code to do math
Constant Summary collapse
- CALCDESC =
the description of this engine boxcar
"will run a ruby calculation to answer a math question"
Instance Attribute Summary
Attributes inherited from Boxcar
#description, #name, #parameters, #return_direct
Instance Method Summary collapse
- #default_params ⇒ Object
-
#initialize(**kwargs) ⇒ RubyCalculator
constructor
A new instance of RubyCalculator.
-
#run(question) ⇒ String
run a ruby calculator question.
Methods inherited from Boxcar
#apply, assi, #call, #conduct, hist, #input_keys, #load, #output_keys, #save, #schema, syst, user, #validate_inputs, #validate_outputs
Constructor Details
#initialize(**kwargs) ⇒ RubyCalculator
Returns a new instance of RubyCalculator.
11 12 13 14 15 16 17 |
# File 'lib/boxcars/boxcar/ruby_calculator.rb', line 11 def initialize(**kwargs) kwargs[:name] ||= "RubyCalculator" kwargs[:description] ||= CALCDESC kwargs[:parameters] ||= default_params super end |
Instance Method Details
#default_params ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/boxcars/boxcar/ruby_calculator.rb', line 19 def default_params { question: { type: :string, description: "a Ruby programming string that will compute the answer to a math question", required: true } } end |
#run(question) ⇒ String
run a ruby calculator question
30 31 32 33 34 35 36 37 |
# File 'lib/boxcars/boxcar/ruby_calculator.rb', line 30 def run(question) code = "puts(#{question})" ruby_executor = Boxcars::RubyREPL.new rv = ruby_executor.call(code: code) puts "Question: #{question}" puts "Answer: #{rv}" rv end |