Skip to content

Commit

Permalink
initial import from bs
Browse files Browse the repository at this point in the history
  • Loading branch information
cldwalker committed Oct 27, 2010
0 parents commit 638a6c4
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/ripl.rb
@@ -0,0 +1,4 @@
require 'ripl/shell'

module Ripl
end
51 changes: 51 additions & 0 deletions lib/ripl/shell.rb
@@ -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.