lib/boxcars/ruby_repl.rb in boxcars-0.2.16 vs lib/boxcars/ruby_repl.rb in boxcars-0.3.1
- old
+ new
@@ -6,20 +6,22 @@
# Execute ruby code
# @param code [String] The code to run
def call(code:)
Boxcars.debug "RubyREPL: #{code}", :yellow
- # wrap the code in an excption block so we can catch errors
+ # wrap the code in an exception block so we can catch errors
wrapped = "begin\n#{code}\nrescue Exception => e\n puts 'Error: ' + e.message\nend"
output = ""
IO.popen("ruby", "r+") do |io|
io.puts wrapped
io.close_write
output = io.read
end
if output =~ /^Error: /
Boxcars.debug output, :red
Result.from_error(output, code: code)
+ elsif output.blank?
+ Result.from_error("The code you gave me did not print a result", code: code)
else
output = ::Regexp.last_match(1) if output =~ /^\s*Answer:\s*(.*)$/m
Boxcars.debug "Answer: #{output}", :yellow, style: :bold
Result.from_text(output, code: code)
end