Skip to content

Instantly share code, notes, and snippets.

@pvdb
Last active August 29, 2015 14:25
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 pvdb/cad2e674a681d311ab80 to your computer and use it in GitHub Desktop.
Save pvdb/cad2e674a681d311ab80 to your computer and use it in GitHub Desktop.
#!/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