module SR
‘sr` is a simple, tiny, hackable REPL for Ruby.
Constants
- CONFIGURATION_FILES
The array of known configuration files for
SR
which should be loaded when the read-eval-print loop is started.- 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.
- VERSION
Semantic version of ‘sr`.
Attributes
@return [Binding] the currently bound SR
context
@return [Hash{Class=>#call}] the hash of exception handlers
@return [#call] the input handler
@return [Hash{Symbol=>#call,#to_s}] the hash of prompts to display; valid
keys are `:error`, `:input`, `:more`, and `:return`; valid values must respond to `#to_s`
Public Class Methods
@param object [#__binding__] the object to bind the SR
context to @return [Binding] the new SR
context
# File lib/sr.rb, line 73 def self.bind(object) @stack.push(@context) @context = object.__binding__ end
Loads the configuration for SR
, resets the default binding to the given ‘context`, and starts the read-eval-print loop.
@param context [#__binding__] the object providing the default context for
`sr`; defaults to the top level binding if no context given
@param options [Hash] the options for the REPL; valid options are
`:config_files` (`Array<String>`) and `:context` (`#__binding__`)
@return [void]
# File lib/sr.rb, line 129 def self.repl(context = nil, **options) options.fetch(:config_files, CONFIGURATION_FILES).each(&method(:load)) context = context || options[:context] reset_binding(context) unless context.nil? @enabled = true read_eval_print while @enabled end
@param context [#__binding__] the context to use as the new default
context; defaults to the top level binding if no context given
@return [Binding] the reset binding context
# 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
@return [Array<Binding>] a copy of the SR
context stack
# File lib/sr.rb, line 67 def self.stack @stack.dup end
@return [Binding] the previous binding in the SR
context stack or the
default context if no previous binding exists
# File lib/sr.rb, line 81 def self.unbind @context = @stack.empty? ? @ORIGINAL_CONTEXT : @stack.pop end
Private Class Methods
@param key [Symbol] the key of the @prompt hash to call or fetch @param args [Array<Object>] the arguments to be passed to the value of the
given key if the value is a proc
@return [String] the prompt to display
# File lib/sr.rb, line 98 def self.call_prompt(key, *args) prompt = @prompt[key] prompt.respond_to?(:call) ? prompt.call(*args) : (prompt || '?? ') end
@param more [String] the previous input to be evaluated along with the
given input
@return [Object] the return value of the given input
# File lib/sr.rb, line 113 def self.read_eval_print(more = nil) value = @context.eval(input = @input_handler.call(more), '(sr)', 0) @context.local_variable_set(:_, value) puts call_prompt(:return, value) if @prompt[:return] rescue *@exception_handlers.keys => ex @exception_handlers.find { |(c,_)| ex.is_a?(c) }[1].call(ex, input, more) end
@param ex [Exception] the exception to report @return [void]
# File lib/sr.rb, line 105 def self.report(ex) @context.local_variable_set(:_, ex) $stderr.puts call_prompt(:error, ex) if @prompt[:error] end