Module: SR

Defined in:
lib/sr.rb,
lib/sr/version.rb

Overview

‘sr` is a simple, tiny, hackable REPL for Ruby.

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-input|tIDENTIFIER/
VERSION =

Semantic version of ‘sr`.

'1.0.0'

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



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

def context
  @context
end

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

Returns the hash of exception handlers.

Returns:

  • (Hash{Class=>#call})

    the hash of exception handlers



13
14
15
# File 'lib/sr.rb', line 13

def handlers
  @handlers
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`



18
19
20
# File 'lib/sr.rb', line 18

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



61
62
63
64
# File 'lib/sr.rb', line 61

def self.bind(object)
  @stack.push(@context)
  @context = object.__binding__
end

.repl(context = nil) ⇒ 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



122
123
124
125
126
127
# File 'lib/sr.rb', line 122

def self.repl(context = nil)
  load_configuration
  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



76
77
78
79
80
# File 'lib/sr.rb', line 76

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



55
56
57
# File 'lib/sr.rb', line 55

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



69
70
71
# File 'lib/sr.rb', line 69

def self.unbind
  @context = @stack.empty? ? @ORIGINAL_CONTEXT : @stack.pop
end