Module: SR
- Defined in:
- lib/sr.rb,
lib/sr/cli.rb,
lib/sr/version.rb,
lib/sr/input/readline.rb
Overview
‘sr` is a simple, tiny, hackable REPL for Ruby.
Defined Under Namespace
Modules: CLI
Constant Summary collapse
- CONFIGURATION_FILES =
The array of known configuration files for SR which should be loaded when the read-eval-print loop is started.
(XDG_CONFIG_DIRS + [XDG_CONFIG_HOME]).map! do |dir| (File.(dir) + '/sr/config.rb').freeze end.select! { |file| File.exist?(file) }.freeze
- CONTINUATION_REGEXP =
The regular expression used to determine whether or not a syntax error is a valid continuation which should prompt for more user input.
/unterminated (?:regexp|string)|unexpected end-of-(?:file|input)|tIDENTIFIER/
- VERSION =
Semantic version of ‘sr`.
'1.1.1'
Class Attribute Summary collapse
-
.context ⇒ Binding
readonly
The currently bound SR context.
-
.exception_handlers ⇒ Hash{Class=>#call}
readonly
The hash of exception handlers.
-
.input_handler ⇒ #call
The input handler.
-
.prompt ⇒ Hash{Symbol=>#call,#to_s}
readonly
The hash of prompts to display; valid keys are ‘:error`, `:input`, `:more`, and `:return`; valid values must respond to `#to_s`.
Class Method Summary collapse
-
.bind(object) ⇒ Binding
(also: context=)
The new SR context.
-
.repl(context = nil, **options) ⇒ void
Loads the configuration for SR, resets the default binding to the given ‘context`, and starts the read-eval-print loop.
-
.reset_binding(context = nil) ⇒ Binding
The reset binding context.
-
.stack ⇒ Array<Binding>
A copy of the SR context stack.
-
.unbind ⇒ Binding
The previous binding in the SR context stack or the default context if no previous binding exists.
Class Attribute Details
.context ⇒ Binding (readonly)
Returns the currently bound SR context.
9 10 11 |
# File 'lib/sr.rb', line 9 def context @context end |
.exception_handlers ⇒ Hash{Class=>#call} (readonly)
Returns the hash of exception handlers.
12 13 14 |
# File 'lib/sr.rb', line 12 def exception_handlers @exception_handlers end |
.input_handler ⇒ #call
Returns the input handler.
15 16 17 |
# File 'lib/sr.rb', line 15 def input_handler @input_handler end |
.prompt ⇒ Hash{Symbol=>#call,#to_s} (readonly)
Returns the hash of prompts to display; valid keys are ‘:error`, `:input`, `:more`, and `:return`; valid values must respond to `#to_s`.
20 21 22 |
# File 'lib/sr.rb', line 20 def prompt @prompt end |
Class Method Details
.bind(object) ⇒ Binding Also known as: context=
Returns the new SR context.
73 74 75 76 |
# File 'lib/sr.rb', line 73 def self.bind(object) @stack.push(@context) @context = object.__binding__ end |
.repl(context = nil, **options) ⇒ void
This method returns an undefined value.
Loads the configuration for SR, resets the default binding to the given ‘context`, and starts the read-eval-print loop.
129 130 131 132 133 134 135 |
# File 'lib/sr.rb', line 129 def self.repl(context = nil, **) .fetch(:config_files, CONFIGURATION_FILES).each(&method(:load)) context = context || [:context] reset_binding(context) unless context.nil? @enabled = true read_eval_print while @enabled end |
.reset_binding(context = nil) ⇒ Binding
Returns the reset binding context.
88 89 90 91 92 |
# File 'lib/sr.rb', line 88 def self.reset_binding(context = nil) @stack.clear context = context.__binding__ unless context.nil? @context = @ORIGINAL_CONTEXT = context ? context : TOPLEVEL_BINDING end |
.stack ⇒ Array<Binding>
Returns a copy of the SR context stack.
67 68 69 |
# File 'lib/sr.rb', line 67 def self.stack @stack.dup end |
.unbind ⇒ Binding
Returns the previous binding in the SR context stack or the default context if no previous binding exists.
81 82 83 |
# File 'lib/sr.rb', line 81 def self.unbind @context = @stack.empty? ? @ORIGINAL_CONTEXT : @stack.pop end |