Skip to content

Instantly share code, notes, and snippets.

@rkh
Created February 17, 2009 15:35
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 rkh/65790 to your computer and use it in GitHub Desktop.
Save rkh/65790 to your computer and use it in GitHub Desktop.
ruby -e "loop { print '>> '; puts('=> ' + eval(gets).inspect) }"
# 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
@michaelmior
Copy link

Small typo on line 31 with a missing +.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment