-
-
Save rkh/65790 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 -e "loop { print '>> '; puts('=> ' + eval(gets).inspect) }" |
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
# A ruby REPL with optional readline support. | |
# Every ruby implementation that supports exception handling | |
# and eval should be able to run this. | |
def get_code(prefix) | |
code = "" | |
begin | |
require "readline" | |
code = Readline::readline prefix | |
Readline::HISTORY.push code | |
rescue Exception # maybe no readline support or some error | |
print prefix | |
code = gets | |
end | |
code | |
end | |
REPL_BINDING = binding if defined? binding | |
loop do | |
result = nil | |
code = get_code ">> " | |
begin | |
if defined? REPL_BINDING | |
result = eval code, REPL_BINDING | |
else | |
result = eval code | |
end | |
rescue Exception => e | |
exit if e.is_a? SystemExit | |
puts(e.class.name + ": " e.to_s) # avoiding "#{..}" | |
end | |
begin | |
puts("=> " + result.inspect) | |
rescue NameError # maybe we don't support inspect | |
puts("=> " + result.to_s) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Small typo on line 31 with a missing
+
.