Module: Ripl::Shell::API

Included in:
Ripl::Shell
Defined in:
lib/ripl/shell.rb

Constant Summary collapse

MESSAGES =
{'prompt' => 'Error while creating prompt',
'print_result' => 'Error while printing result'}

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#promptString

Returns:

  • (String)


91
92
93
# File 'lib/ripl/shell.rb', line 91

def prompt
  @prompt
end

#result_promptObject

Returns the value of attribute result_prompt.



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

def result_prompt
  @result_prompt
end

Instance Method Details

#add_commands(obj) ⇒ Object



52
53
54
55
# File 'lib/ripl/shell.rb', line 52

def add_commands(obj)
  ![Symbol, Fixnum].include?(obj.class) ? obj.extend(Ripl::Commands) :
    obj.class.send(:include, Ripl::Commands)
end

#after_loopObject

Called after shell finishes looping.



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

def after_loop; end

#before_loopObject

Sets up shell before looping by loading ~/.irbrc. Can be extended to initialize plugins and their instance variables.



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

def before_loop
  Ripl::Runner.load_rc(@irbrc) if @irbrc
  add_commands(eval("self", @binding))
end

#eval_input(input) ⇒ Object

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



72
73
74
75
76
77
78
79
80
# File 'lib/ripl/shell.rb', line 72

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

#format_error(err) ⇒ String

Formats errors raised by eval of user input

Parameters:

  • (Exception)

Returns:

  • (String)


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

def format_error(err) Ripl::Runner.format_error(err) end

#format_result(result) ⇒ String

Returns Formats result using result_prompt.

Returns:

  • (String)

    Formats result using result_prompt



123
124
125
# File 'lib/ripl/shell.rb', line 123

def format_result(result)
  result_prompt + result.inspect
end

#get_inputString?

When extending this method, ensure your plugin disables readline: Readline.config = false.

Returns:

  • (String, nil)

    Prints #prompt and returns input given by user



85
86
87
88
# File 'lib/ripl/shell.rb', line 85

def get_input
  print prompt
  (input = $stdin.gets) ? input.chomp : input
end

#handle_interruptObject

Handles interrupt (Control-C) by printing a newline



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

def handle_interrupt() puts end

#in_loopObject



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

def in_loop
  catch(:ripl_exit) { loop_once while(true) }
end

#loop_eval(str) ⇒ Object

Evals user input using @binding, @name and @line



99
100
101
# File 'lib/ripl/shell.rb', line 99

def loop_eval(str)
  eval(str, @binding, "(#{@name})", @line)
end

#loop_onceObject

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



58
59
60
61
62
63
64
65
66
# File 'lib/ripl/shell.rb', line 58

def loop_once
  @error_raised = nil
  @input = get_input
  throw(:ripl_exit) if EXIT_WORDS.include?(@input)
  eval_input(@input)
  print_result(@result)
rescue Interrupt
  handle_interrupt
end

Prints error formatted by #format_error to STDERR. Could be extended to handle certain exceptions.

Parameters:

  • (Exception)


106
107
108
# File 'lib/ripl/shell.rb', line 106

def print_eval_error(err)
  warn format_error(err)
end

Prints result using #format_result



111
112
113
114
115
# File 'lib/ripl/shell.rb', line 111

def print_result(result)
  puts(format_result(result)) unless @error_raised
rescue StandardError, SyntaxError
  warn "ripl: #{MESSAGES['print_result']}:\n"+ format_error($!)
end