module Ripl::Shell::API

Constants

MESSAGES

Attributes

prompt[RW]
result_prompt[RW]

Public Instance Methods

add_commands(obj) click to toggle source
# File lib/ripl/shell.rb, line 59
def add_commands(obj)
  ![Symbol, Fixnum].include?(obj.class) ? obj.extend(Ripl::Commands) :
    obj.class.send(:include, Ripl::Commands)
end
after_loop() click to toggle source

Called after shell finishes looping.

# File lib/ripl/shell.rb, line 135
def after_loop; end
before_loop() click to toggle source

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

# File lib/ripl/shell.rb, line 50
def before_loop
  Ripl::Runner.load_rc(@irbrc) if @irbrc
  add_commands(eval("self", @binding))
end
eval_input(input) click to toggle source

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

# File lib/ripl/shell.rb, line 79
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) click to toggle source

Formats errors raised by eval of user input @param [Exception] @return [String]

# File lib/ripl/shell.rb, line 127
def format_error(err) Ripl::Runner.format_error(err) end
format_result(result) click to toggle source

@return [String] Formats result using result_prompt

# File lib/ripl/shell.rb, line 130
def format_result(result)
  result_prompt + result.inspect
end
get_input() click to toggle source

When extending this method, ensure your plugin disables readline: Readline.config = false. @return [String, nil] Prints prompt and returns input given by user

# File lib/ripl/shell.rb, line 92
def get_input
  print prompt
  (input = $stdin.gets) ? input.chomp : input
end
handle_interrupt() click to toggle source

Handles interrupt (Control-C) by printing a newline

# File lib/ripl/shell.rb, line 76
def handle_interrupt() puts end
in_loop() click to toggle source
# File lib/ripl/shell.rb, line 55
def in_loop
  catch(:ripl_exit) { loop_once while(true) }
end
loop_eval(str) click to toggle source

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

# File lib/ripl/shell.rb, line 106
def loop_eval(str)
  eval(str, @binding, "(#{@name})", @line)
end
loop_once() click to toggle source

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

# File lib/ripl/shell.rb, line 65
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
print_eval_error(err) click to toggle source

Prints error formatted by format_error to STDERR. Could be extended to handle certain exceptions. @param [Exception]

print_result(result) click to toggle source

Prints result using format_result