Permalink
Browse files

initial import from bs

  • Loading branch information...
0 parents commit 638a6c4f34573cb75480188c344c8737f7f0ffc4 @cldwalker committed Oct 27, 2010
Showing with 55 additions and 0 deletions.
  1. +4 −0 lib/ripl.rb
  2. +51 −0 lib/ripl/shell.rb
@@ -0,0 +1,4 @@
+require 'ripl/shell'
+
+module Ripl
+end
@@ -0,0 +1,51 @@
+module Ripl
+ class Shell
+ OPTIONS = {:name=>'ripl', :line=>1, :result_prompt=>'=> ',
+ :binding=>TOPLEVEL_BINDING, :irbrc=>'~/.irbrc'}
+
+ attr_accessor :line, :binding, :result_prompt
+ attr_reader :has_autocompletion
+ def initialize(options={})
+ @options = OPTIONS.merge options
+ @name, @binding, @line = @options.values_at(:name, :binding, :line)
+ start_completion
+ load_rc
+ end
+
+ def start_completion
+ require 'bond'
+ Bond.start
+ @has_autocompletion = true
+ rescue LoadError
+ @has_autocompletion = false
+ end
+
+ def load_rc
+ load @options[:irbrc] if File.exists?(File.expand_path(@options[:irbrc]))
+ end
+
+ def eval_line(str)
+ eval(str, @binding, "(#{@name})", @line)
+ end
+
+ def print_eval_error(e)
+ warn "#{e.class}: #{e.message}"
+ end
+
+ def format_result(result)
+ @options[:result_prompt] + result.inspect
+ end
+
+ def loop_once(input)
+ begin
+ result = eval_line(input)
+ rescue Exception => e
+ print_eval_error(e)
+ end
+
+ eval("_ = #{result.inspect}", @binding) rescue nil
+ @line += 1
+ format_result result
+ end
+ end
+end

0 comments on commit 638a6c4

Please sign in to comment.