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.expand_path(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

Class Method Summary collapse

Class Attribute Details

.contextBinding (readonly)

Returns the currently bound SR context.

Returns:

  • (Binding)

    the currently bound SR context



9
10
11
# File 'lib/sr.rb', line 9

def context
  @context
end

.exception_handlersHash{Class=>#call} (readonly)

Returns the hash of exception handlers.

Returns:

  • (Hash{Class=>#call})

    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.

Returns:

  • (#call)

    the input handler



15
16
17
# File 'lib/sr.rb', line 15

def input_handler
  @input_handler
end

.promptHash{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`.

Returns:

  • (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`



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.

Parameters:

  • object (#__binding__)

    the object to bind the SR context to

Returns:

  • (Binding)

    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.

Parameters:

  • context (#__binding__) (defaults to: nil)

    the object providing the default context for ‘sr`; defaults to the top level binding if no context given

  • options (Hash)

    the options for the REPL; valid options are ‘:config_files` (`Array<String>`) and `:context` (`#__binding__`)



129
130
131
132
133
134
135
# 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

.reset_binding(context = nil) ⇒ Binding

Returns the reset binding context.

Parameters:

  • context (#__binding__) (defaults to: nil)

    the context to use as the new default context; defaults to the top level binding if no context given

Returns:

  • (Binding)

    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

.stackArray<Binding>

Returns a copy of the SR context stack.

Returns:

  • (Array<Binding>)

    a copy of the SR context stack



67
68
69
# File 'lib/sr.rb', line 67

def self.stack
  @stack.dup
end

.unbindBinding

Returns the previous binding in the SR context stack or the default context if no previous binding exists.

Returns:

  • (Binding)

    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