Module: Rib::API
- Included in:
- Shell
- Defined in:
- lib/rib/api.rb
Class Method Summary collapse
-
.format_error(err) ⇒ Object
Format error raised in #loop_eval with #get_error.
-
.get_error(err) ⇒ Object
Get error message and backtrace from a particular error.
Instance Method Summary collapse
-
#after_loop ⇒ Object
Called after shell finishes looping.
-
#before_loop ⇒ Object
Called before shell starts looping.
-
#bound_object ⇒ Object
The object for the current binding.
-
#eval_binding ⇒ Object
The binding for evaluation.
-
#eval_input(input) ⇒ Object
Evaluate the input using #loop_eval and handle it.
-
#format_result(result) ⇒ Object
Format result using #result_prompt.
-
#get_input ⇒ Object
Get user input.
-
#handle_interrupt ⇒ Object
Handle interrupt (control-c).
-
#in_loop ⇒ Object
Main loop.
-
#line ⇒ Object
The line number for next evaluation.
-
#loop_eval(input) ⇒ Object
Evaluate user input with #eval_binding, name and line.
-
#loop_once ⇒ Object
Loop iteration: REPL.
-
#name ⇒ Object
The name of this shell.
-
#print_eval_error(err) ⇒ Object
Print evaluated error using #format_error.
-
#print_result(result) ⇒ Object
Print result using #format_result.
-
#prompt ⇒ Object
The prompt string of this shell.
-
#result_prompt ⇒ Object
The result prompt string of this shell.
Class Method Details
.format_error(err) ⇒ Object
Format error raised in #loop_eval with #get_error
100 101 102 103 |
# File 'lib/rib/api.rb', line 100 def format_error err , backtrace = get_error(err) "#{}\n #{backtrace.join("\n ")}" end |
.get_error(err) ⇒ Object
Get error message and backtrace from a particular error
107 108 109 |
# File 'lib/rib/api.rb', line 107 def get_error err ["#{err.class}: #{err.}", err.backtrace] end |
Instance Method Details
#after_loop ⇒ Object
Called after shell finishes looping
10 11 12 |
# File 'lib/rib/api.rb', line 10 def after_loop self end |
#before_loop ⇒ Object
Called before shell starts looping
5 6 7 |
# File 'lib/rib/api.rb', line 5 def before_loop self end |
#bound_object ⇒ Object
The object for the current binding
27 28 29 |
# File 'lib/rib/api.rb', line 27 def bound_object config[:bound_object] ||= eval_binding.eval('self', __FILE__, __LINE__) end |
#eval_binding ⇒ Object
The binding for evaluation
23 |
# File 'lib/rib/api.rb', line 23 def eval_binding ; config[:binding] ; end |
#eval_input(input) ⇒ Object
Evaluate the input using #loop_eval and handle it
65 66 67 68 69 70 71 72 73 |
# File 'lib/rib/api.rb', line 65 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_result(result) ⇒ Object
Format result using #result_prompt
95 96 97 |
# File 'lib/rib/api.rb', line 95 def format_result result result_prompt + result.inspect end |
#get_input ⇒ Object
Get user input. This is most likely overrided in Readline plugin
55 56 57 58 59 60 61 62 |
# File 'lib/rib/api.rb', line 55 def get_input print(prompt) if input = $stdin.gets input.chomp else nil end end |
#handle_interrupt ⇒ Object
Handle interrupt (control-c)
15 |
# File 'lib/rib/api.rb', line 15 def handle_interrupt; puts ; end |
#in_loop ⇒ Object
Main loop
32 33 34 35 |
# File 'lib/rib/api.rb', line 32 def in_loop input = catch(:rib_exit){ loop_once while true } puts if input == nil end |
#line ⇒ Object
The line number for next evaluation
25 |
# File 'lib/rib/api.rb', line 25 def line ; config[:line] ; end |
#loop_eval(input) ⇒ Object
Evaluate user input with #eval_binding, name and line
76 77 78 |
# File 'lib/rib/api.rb', line 76 def loop_eval input eval_binding.eval(input, "(#{name})", line) end |
#loop_once ⇒ Object
Loop iteration: REPL
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/rib/api.rb', line 38 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 [result, err] rescue Interrupt handle_interrupt end |
#name ⇒ Object
The name of this shell
21 |
# File 'lib/rib/api.rb', line 21 def name ; config[:name] ; end |
#print_eval_error(err) ⇒ Object
Print evaluated error using #format_error
88 89 90 91 92 |
# File 'lib/rib/api.rb', line 88 def print_eval_error err puts(format_error(err)) rescue StandardError, SyntaxError => e Rib.warn("Error while printing error:\n #{format_error(e)}") end |
#print_result(result) ⇒ Object
Print result using #format_result
81 82 83 84 85 |
# File 'lib/rib/api.rb', line 81 def print_result result puts(format_result(result)) rescue StandardError, SyntaxError => e Rib.warn("Error while printing result:\n #{format_error(e)}") end |
#prompt ⇒ Object
The prompt string of this shell
17 |
# File 'lib/rib/api.rb', line 17 def prompt ; config[:prompt] ; end |
#result_prompt ⇒ Object
The result prompt string of this shell
19 |
# File 'lib/rib/api.rb', line 19 def result_prompt; config[:result_prompt] ; end |