module Ripl::Shell::API
Constants
- MESSAGES
Attributes
Public Instance Methods
# 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
Called after shell finishes looping.
# File lib/ripl/shell.rb, line 135 def after_loop; end
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
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
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
@return [String] Formats result using result_prompt
# File lib/ripl/shell.rb, line 130 def format_result(result) result_prompt + result.inspect end
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
Handles interrupt (Control-C) by printing a newline
# File lib/ripl/shell.rb, line 76 def handle_interrupt() puts end
# File lib/ripl/shell.rb, line 55 def in_loop catch(:ripl_exit) { loop_once while(true) } end
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
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
Prints error formatted by format_error
to STDERR. Could be extended to handle certain exceptions. @param [Exception]
# File lib/ripl/shell.rb, line 113 def print_eval_error(err) $stderr.puts format_error(err) end
Prints result using format_result
# File lib/ripl/shell.rb, line 118 def print_result(result) puts(format_result(result)) unless @error_raised rescue StandardError, SyntaxError $stderr.puts "ripl: #{MESSAGES['print_result']}:", format_error($!) end