Last active
August 29, 2015 14:25
-
-
Save pvdb/cad2e674a681d311ab80 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
#!/usr/bin/env ruby | |
require 'pp' | |
def ruby_repl processor = nil | |
unless (processor && Proc === processor) || block_given? | |
warn "Please invoke `ruby_repl` with a block or a proc..." | |
else | |
in_pry = Kernel.const_defined?("Pry::ColorPrinter") | |
cmd_count = 0 # for use in the command prompt | |
loop do | |
print "repl(%s):%d> " % [ | |
(in_pry ? Pry.view_clip(self) : self.inspect), | |
(cmd_count += 1), | |
] | |
input = gets | |
( puts ; break ) unless input | |
input = input.chomp | |
begin | |
result = processor ? processor.call(input) : yield(input) | |
print "=> " | |
in_pry ? Pry::ColorPrinter.pp(result) : PP.pp(result) | |
rescue | |
puts "%s: %s" % [$!.class.name, $!.message] | |
puts "from (repl):#{cmd_count}:in `__ruby_repl__'" | |
end | |
end | |
end | |
end | |
alias :repl :ruby_repl | |
alias :rr :ruby_repl | |
if __FILE__ == $0 | |
require 'pry' rescue LoadError | |
ruby_repl do |input| input ; end | |
end | |
# That's all Folks! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment