sr

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

Usage

Simply call sr to use sr as a Ruby REPL:

$ sr

You may also set the default sr context to any valid object using the '-c' or '--context' option:

$ sr --context Array.new
# -- OR --
$ sr -cArray.new

Any arguments to be passed to Ruby itself may be given via the $RUBYOPT environment variable. Common options (such as '-I' for manipulating the load path or '-r' to require libraries) may be given in this manner:

$ RUBYOPT=-ryaml sr -cPsych

sr can also be used to include a REPL in your own code (for debugging purposes, for example) by requiring 'sr' and calling SR.repl:

require 'sr'

class Test
  def initialize
    SR.repl
  end
end

You may also pass any object to the SR.repl method to have sr begin in the context of that object instead of the default (the top level binding):

class Test
  def initialize
    SR.repl(self)
  end
end

Within the REPL itself, you can manipulate the current context via the methods SR.bind, SR.unbind, and SR.reset_binding:

$ sr
== main:
>> SR.bind Object.new
-> #<Binding:0x81338ed0>
== #<Object:0x81338f34>:
>> SR.unbind
-> #<Binding:0x8124dc78>
== main:
>> 

SR.bind sets the current context to the given object and pushes the previous context onto the context stack; SR.unbind retrieves the last object from the stack and sets the context to it. SR.reset_binding may be used without arguments at any time to completely clear the context stack and reset the binding to the default (usually the top level binding).

When given an argument, however, SR.reset_binding clears the context stack and resets the default binding to the given object.

Configuration

sr looks for its configuration in the 'sr/config.rb' file within $XDG_CONFIG_DIRS and $XDG_CONFIG_HOME. By default, these files would be '/etc/xdg/sr/config.rb' for system-wide configuration and '~/.config/sr/config.rb' for user-specific configuration. These files are simply Ruby files which are loaded into sr via Kernel#load.

You may also tell sr to load alternative configuration files (or suppress all configuration) with the '-f' option. Given no arguments, this option suppresses all configuration loading; given a file, it removes the default configuration files and adds the given file to the list of configuration files to load. This option may be specified multiple times to load multiple alternative files.

$ sr -f        # start with no configuration
$ sr -f alt.rb # start with alt.rb as configuration

In addition to this, sr simply uses STDIN.gets as its default input method; if you would prefer to use Readline, simply add the following to your sr configuration file:

require 'sr/input/readline'

Alternatively, to use Readline temporarily, simply use $RUBYOPT:

$ RUBYOPT=-rsr/input/readline sr

License

sr is free and unencumbered software released into the public domain.