Class: Boxcars::RubyCalculator

Inherits:
Boxcar
  • Object
show all
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

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.

Parameters:

  • kwargs (Hash)

    Any other keyword arguments to pass to the parent class.



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_paramsObject



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

Parameters:

  • question (String)

    The question to ask Google.

Returns:

  • (String)

    The answer to the 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