module Rib::API
Public Class Methods
format_error(err)
click to toggle source
get_error(err)
click to toggle source
Get error message and backtrace from a particular error
# File lib/rib/api.rb, line 128 def get_error err ["#{err.class}: #{err.message}", format_backtrace(err.backtrace)] end
Public Instance Methods
after_loop()
click to toggle source
Called after shell finishes looping
# File lib/rib/api.rb, line 9 def after_loop self end
before_loop()
click to toggle source
Called before shell starts looping
# File lib/rib/api.rb, line 4 def before_loop self end
eval_binding()
click to toggle source
The binding for evaluation
# File lib/rib/api.rb, line 22 def eval_binding ; config[:binding] ; end
eval_input(input)
click to toggle source
Evaluate the input using loop_eval
and handle it
# File lib/rib/api.rb, line 67 def eval_input input [loop_eval(input), nil] rescue SystemExit throw(:rib_exit, input) rescue Exception => e [nil, e] ensure config[:line] += 1 end
format_backtrace(backtrace)
click to toggle source
# File lib/rib/api.rb, line 133 def format_backtrace backtrace backtrace end
format_result(result)
click to toggle source
Format result using result_prompt
# File lib/rib/api.rb, line 109 def format_result result "#{result_prompt}#{inspect_result(result)}" end
get_input()
click to toggle source
Get user input. This is most likely overrided in Readline
plugin
# File lib/rib/api.rb, line 57 def get_input print(prompt) if input = $stdin.gets input.chomp else nil end end
handle_interrupt()
click to toggle source
Handle interrupt (control-c)
# File lib/rib/api.rb, line 14 def handle_interrupt; puts ; end
in_loop()
click to toggle source
Main loop
# File lib/rib/api.rb, line 29 def in_loop input = catch(:rib_exit){ loop_once while running? } puts if input == nil && running? end
inspect_result(result)
click to toggle source
# File lib/rib/api.rb, line 113 def inspect_result result string = result.inspect warn("#{result.class}#inspect is not returning a string") unless string.kind_of?(String) string end
line()
click to toggle source
The line number for next evaluation
# File lib/rib/api.rb, line 24 def line ; config[:line] ; end
loop_eval(input)
click to toggle source
Evaluate user input with eval_binding
, name and line
# File lib/rib/api.rb, line 78 def loop_eval input if eval_binding.kind_of?(Binding) eval_binding.eval(input, "(#{name})", line) else eval_binding.instance_eval(input, "(#{name})", line) end end
loop_once()
click to toggle source
Loop iteration: REPL
# File lib/rib/api.rb, line 35 def loop_once input, result, err = get_input, nil, nil throw(:rib_exit, input) if config[:exit].include?(input) result, err = eval_input(input) if err print_eval_error(err) elsif input.strip != '' && !equal_rib_skip(result) print_result(result) else # print nothing for blank input or Rib::Skip end flush_warnings [result, err] rescue Interrupt handle_interrupt end
name()
click to toggle source
The name of this shell
# File lib/rib/api.rb, line 20 def name ; config[:name] ; end
print_eval_error(err)
click to toggle source
Print evaluated error using format_error
# File lib/rib/api.rb, line 98 def print_eval_error err puts(format_error(err)) rescue StandardError, SyntaxError => e warn("Error while printing error:\n #{format_error(e)}") end
print_result(result)
click to toggle source
Print result using format_result
# File lib/rib/api.rb, line 91 def print_result result puts(format_result(result)) rescue StandardError, SyntaxError => e warn("Error while printing result:\n #{format_error(e)}") end
prompt()
click to toggle source
The prompt string of this shell
# File lib/rib/api.rb, line 16 def prompt ; config[:prompt] ; end
puts(str='')
click to toggle source
Calls superclass method
# File lib/rib/api.rb, line 86 def puts str='' super end
result_prompt()
click to toggle source
The result prompt string of this shell
# File lib/rib/api.rb, line 18 def result_prompt; config[:result_prompt] ; end
started_at()
click to toggle source
When the application loaded
# File lib/rib/api.rb, line 26 def started_at ; config[:started_at] ; end
warn(message)
click to toggle source
# File lib/rib/api.rb, line 104 def warn message warnings << message end
Private Instance Methods
equal_rib_skip(result)
click to toggle source
# File lib/rib/api.rb, line 138 def equal_rib_skip result result == Rib::Skip rescue # do nothing, it cannot respond to == correctly, it can't be Rib::Skip end
flush_warnings()
click to toggle source
# File lib/rib/api.rb, line 144 def flush_warnings Rib.warn(warnings.shift) until warnings.empty? end
format_error(err)
click to toggle source
get_error(err)
click to toggle source
Get error message and backtrace from a particular error
# File lib/rib/api.rb, line 128 def get_error err ["#{err.class}: #{err.message}", format_backtrace(err.backtrace)] end