Class: Ripl::Shell

Inherits:
Object
  • Object
show all
Defined in:
lib/ripl/shell.rb

Direct Known Subclasses

ReadlineShell

Constant Summary collapse

OPTIONS =
{:name=>'ripl', :line=>1, :result_prompt=>'=> ', :prompt=>'>> ',
:binding=>TOPLEVEL_BINDING, :irbrc=>'~/.irbrc', :history=>'~/.irb_history'}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Shell

Returns a new instance of Shell.



7
8
9
10
11
# File 'lib/ripl/shell.rb', line 7

def initialize(options={})
  @options = OPTIONS.merge options
  @name, @binding, @line = @options.values_at(:name, :binding, :line)
  @irbrc = @options[:irbrc]
end

Instance Attribute Details

#bindingObject

Returns the value of attribute binding.



6
7
8
# File 'lib/ripl/shell.rb', line 6

def binding
  @binding
end

#lineObject

Returns the value of attribute line.



6
7
8
# File 'lib/ripl/shell.rb', line 6

def line
  @line
end

#result_promptObject

Returns the value of attribute result_prompt.



6
7
8
# File 'lib/ripl/shell.rb', line 6

def result_prompt
  @result_prompt
end

Instance Method Details

#format_error(e) ⇒ Object



43
44
45
# File 'lib/ripl/shell.rb', line 43

def format_error(e)
  "#{e.class}: #{e.message}\n\t#{e.backtrace.join("\n\t")}"
end

#format_result(result) ⇒ Object



47
48
49
# File 'lib/ripl/shell.rb', line 47

def format_result(result)
  @options[:result_prompt] + result.inspect
end

#get_inputObject



23
24
25
26
# File 'lib/ripl/shell.rb', line 23

def get_input
  print @options[:prompt]
  $stdin.gets.chomp
end

#loopObject



13
14
15
16
17
18
19
20
21
# File 'lib/ripl/shell.rb', line 13

def loop
  before_loop
  while true do
    input = get_input
    break if !input || input == 'exit'
    puts loop_once(input)
  end
  after_loop
end

#loop_once(input) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/ripl/shell.rb', line 28

def loop_once(input)
  begin
    result = loop_eval(input)
  rescue Exception => e
    print_eval_error(e)
  end

  @line += 1
  format_result result
end


39
40
41
# File 'lib/ripl/shell.rb', line 39

def print_eval_error(e)
  warn format_error(e)
end