Last active
September 26, 2018 14:17
-
-
Save universal/46ab962981bde20a4d9ffad3ab71e061 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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=>""} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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