Class: Ripl::Shell

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

Defined Under Namespace

Modules: API

Constant Summary collapse

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from API

#after_loop, #before_loop, #format_error, #format_result, #get_input, #loop_eval, #print_eval_error, #print_result, #prompt

Constructor Details

#initialize(options = {}) ⇒ Shell

Returns a new instance of Shell.



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

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

Instance Attribute Details

#bindingObject

Returns the value of attribute binding.



13
14
15
# File 'lib/ripl/shell.rb', line 13

def binding
  @binding
end

#lineObject

Returns the value of attribute line.



13
14
15
# File 'lib/ripl/shell.rb', line 13

def line
  @line
end

#nameObject

Returns the value of attribute name.



13
14
15
# File 'lib/ripl/shell.rb', line 13

def name
  @name
end

#resultObject

Returns the value of attribute result.



13
14
15
# File 'lib/ripl/shell.rb', line 13

def result
  @result
end

#result_promptObject

Returns the value of attribute result_prompt.



13
14
15
# File 'lib/ripl/shell.rb', line 13

def result_prompt
  @result_prompt
end

Class Method Details

.create(options = {}) ⇒ Object



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

def self.create(options={})
  require 'ripl/readline' if options[:readline]
  require 'ripl/completion'
  new(options)
rescue LoadError
  new(options)
end

Instance Method Details

#configObject



28
# File 'lib/ripl/shell.rb', line 28

def config; Ripl.config; end

#eval_input(input) ⇒ Object

Sets @result to result of evaling input and print unexpected errors



40
41
42
43
44
45
46
47
48
# File 'lib/ripl/shell.rb', line 40

def eval_input(input)
  @result = loop_eval(input)
  eval("_ = Ripl.shell.result", @binding)
rescue Exception => e
  @error_raised = true
  print_eval_error(e)
ensure
  @line += 1
end

#loopObject

Loops shell until user exits



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

def loop
  before_loop
  catch(:ripl_exit) { while(true) do; loop_once; end }
  after_loop
end

#loop_onceObject

Runs through one loop iteration: gets input, evals and prints result



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

def loop_once
  @error_raised = nil
  @input = get_input
  throw(:ripl_exit) if !@input || @input == 'exit'
  eval_input(@input)
  print_result(@result)
end