Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/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!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement