Skip to content

Instantly share code, notes, and snippets.

@universal
Last active September 26, 2018 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save universal/46ab962981bde20a4d9ffad3ab71e061 to your computer and use it in GitHub Desktop.
Save universal/46ab962981bde20a4d9ffad3ab71e061 to your computer and use it in GitHub Desktop.
ruby test.rb 'puts "42"'
evaluating next: puts "42"
{:result=>nil, :output=>"42\n"}
ruby test.rb '1+1'
evaluating next: 1+1
{:result=>2, :output=>""}
code = ARGV[0]
puts "evaluating next: #{code}"
require 'stringio'
require 'pp'
new_stdout = StringIO.new
old_stdout = $stdout
$stdout = new_stdout
result = eval code
out = { result: result, output: new_stdout.string}
$stdout = old_stdout
pp out
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment