Pry is a powerful alternative to the standard IRB shell for Ruby. It features syntax highlighting, a flexible plugin architecture, runtime invocation and source and documentation browsing.
Pry can be started similar to other command line utilities by simply running the following command:
pry
Once inside Pry you can invoke the help message:
help
This will show a list of available commands and their usage. For more information about Pry you can refer to the following resources:
DEFAULT_HOOKS | = | Pry::Hooks.new.add_hook(:before_session, :default) do |out, target, _pry_| next if _pry_.quiet? | The default hooks - display messages when beginning and ending Pry sessions. | |
RC_FILES | = | ["~/.pryrc"] | The RC Files to load. | |
LOCAL_RC_FILE | = | "./.pryrc" | ||
VERSION | = | "0.9.10" | ||
Commands | = | Pry::CommandSet.new do import DefaultCommands::Misc | Default commands used by Pry. | |
DEFAULT_CUSTOM_COMPLETIONS | = | proc { commands.commands.keys } | This proc will be instance_eval‘s against the active Pry instance | |
FILE_COMPLETIONS | = | proc { commands.commands.keys + Dir.entries('.') } |
quiet | -> | quiet? |
backtrace | [RW] | |
binding_stack | [RW] | |
cli | [RW] | @return [Boolean] Whether Pry was activated from the command line. |
command_state | [R] | This is exposed via Pry::Command#state. |
commands | [RW] | |
config | [RW] | @return [OpenStruct] Return Pry‘s config object. |
current_line | [RW] | @return [Fixnum] The current input line. |
custom_completions | [RW] |
Get/Set the Proc that defines extra Readline completions (on top of the
ones defined for IRB). @return [Proc] The Proc that defines extra Readline
completions (on top @example Add file names to completion list
Pry.custom_completions = proc { Dir.entries('.') } |
custom_completions | [RW] | |
eval_path | [RW] |
@return [String] The FILE for the `eval()`. Should be
"(pry)"
by default. |
exception_handler | [RW] | |
extra_sticky_locals | [RW] | |
history | [RW] | @return [History] Return Pry‘s line history object. |
hooks | [R] | Special treatment for hooks as we want to alert people of the changed API |
input | [RW] | |
input_array | [R] | |
input_stack | [RW] | |
last_dir | [RW] | |
last_exception | [R] | |
last_file | [RW] | |
last_result | [RW] | |
line_buffer | [RW] | @return [Array] The Array of evaluated expressions. |
output | [RW] | |
output_array | [R] | |
[RW] | ||
quiet | [RW] | @return [Boolean] Whether Pry sessions are quiet by default. |
quiet | [RW] | |
suppress_output | [RW] | |
toplevel_binding | [RW] | @return [Binding] A top level binding with no local variables |
Convert the given object into an instance of `Pry::Code`, if it isn‘t already one.
@param [Code, Method, UnboundMethod, Proc, Pry::Method, String, Array,
IO] obj
If the given object is a `Pry::Method`, return it unaltered. If it‘s anything else, return it wrapped in a `Pry::Method` instance.
If the given object is a `Pry::WrappedModule`, return it unaltered. If it‘s anything else, return it wrapped in a `Pry::WrappedModule` instance.
Return a `Binding` object for `target` or return `target` if it is already a `Binding`. In the case where `target` is top-level then return `TOPLEVEL_BINDING` @param [Object] target The object to get a `Binding` object for. @return [Binding] The `Binding` object.
To avoid mass-confusion, we change the default colour of "white" to "blue" enabling global legibility
@return [Boolean] Whether this is the first time a Pry session has
been started since loading the Pry class.
Do basic setup for initial session. Including: loading .pryrc, loading plugins, loading requires, and loading history.
Load the given file in the context of `Pry.toplevel_binding` @param [String] file_name The unexpanded file path.
Execute the file through the REPL loop, non-interactively. @param [String] file_name File name to load through the REPL.
Load the rc files given in the `Pry::RC_FILES` array. This method can also be used to reload the files if they have changed.
Create a new `Pry` object. @param [Hash] options The optional configuration parameters. @option options [readline] :input The object to use for input. @option options [puts] :output The object to use for output. @option options [Pry::CommandBase] :commands The object to use for commands. @option options [Hash] :hooks The defined hook Procs @option options [Array<Proc>] :prompt The array of Procs to use for the prompts. @option options [Proc] :print The Proc to use for the ‘print’ @option options [Boolean] :quiet If true, omit the whereami banner when starting.
component of the REPL. (see print.rb)
Run a Pry command from outside a session. The commands available are those referenced by `Pry.commands` (the default command set). @param [String] command_string The Pry command (including arguments,
if any).
@param [Hash] options Optional named parameters. @return [Object] The return value of the Pry command. @option options [Object, Binding] :context The object context to run the
command under. Defaults to `TOPLEVEL_BINDING` (main).
@option options [Boolean] :show_output Whether to show command
output. Defaults to true.
@example Run at top-level with no output.
Pry.run_command "ls"
@example Run under Pry class, returning only public methods.
Pry.run_command "ls -m", :context => Pry
@example Display command output.
Pry.run_command "ls -av", :show_output => true
An inspector that clips the output to `max_length` chars. In case of > `max_length` chars the `#<Object…> notation is used. @param obj The object to view. @param max_length The maximum number of chars before clipping occurs. @return [String] The string representation of `obj`.
Add a sticky local to this Pry instance. A sticky local is a local that persists between all bindings in a session. @param [Symbol] name The name of the sticky local. @yield The block that defines the content of the local. The local
will be refreshed at each tick of the repl loop.
Execute the specified hook. @param [Symbol] name The hook name to execute @param [*Object] args The arguments to pass to the hook @return [Object, Exception] The return value of the hook or the exception raised
If executing a hook raises an exception, we log that and then continue sucessfully. To debug such errors, use the global variable $pry_hook_error, which is set as a result.
FIXME: This is a hack to alert people of the new API. @param [Pry::Hooks] v Only accept `Pry::Hooks` now!
@return [Boolean] True if the last result is an exception that was raised,
as opposed to simply an instance of Exception (like the result of Exception.new)
@return [Integer] The maximum amount of objects remembered by the inp and
out arrays. Defaults to 100.
Pops the current prompt off of the prompt stack. If the prompt you are popping is the last prompt, it will not be popped. Use this to restore the previous prompt. @return [Array<Proc>] Prompt being popped. @example
prompt1 = [ proc { '>' }, proc { '>>' } ] prompt2 = [ proc { '$' }, proc { '>' } ] pry = Pry.new :prompt => prompt1 pry.push_prompt(prompt2) pry.pop_prompt # => prompt2 pry.pop_prompt # => prompt1 pry.pop_prompt # => prompt1
If the given line is a valid command, process it in the context of the current `eval_string` and context. This method should not need to be invoked directly. @param [String] val The line to process. @param [String] eval_string The cumulative lines of input. @param [Binding] target The target of the Pry session. @return [Boolean] `true` if `val` is a command, `false` otherwise
Perform a read. If no parameter is given, default to top-level (main). This is a multi-line read; so the read continues until a valid Ruby expression is received. Pry commands are also accepted here and operate on the target. @param [Object, Binding] target The receiver of the read. @param [String] eval_string Optionally Prime `eval_string` with a start value. @return [String] The Ruby expression. @example
Pry.new.r(Object.new)
Raise an exception out of Pry.
See Kernel#raise for documentation of parameters. See rb_make_exception for the inbuilt implementation.
This is necessary so that the raise-up command can tell the difference between an exception the user has decided to raise, and a mistake in specifying that exception.
(i.e. raise-up RunThymeError.new should not be the same as
raise-up NameError, "unititialized constant RunThymeError")
Perform a read-eval If no parameter is given, default to top-level (main). @param [Object, Binding] target The receiver of the read-eval-print @return [Object] The result of the eval or an `Exception` object in case of
error. In the latter case, you can check whether the exception was raised or is just the result of the expression using #last_result_is_exception?
@example
Pry.new.re(Object.new)
Returns the next line of input to be used by the pry instance. This method should not need to be invoked directly. @param [String] current_prompt The prompt to use for input. @return [String] The next line of input.
Perform a read-eval-print. If no parameter is given, default to top-level (main). @param [Object, Binding] target The receiver of the read-eval-print @example
Pry.new.rep(Object.new)
Start a read-eval-print-loop. If no parameter is given, default to top-level (main). @param [Object, Binding] target The receiver of the Pry session @return [Object] The target of the Pry session or an explictly given
return value. If given return value is `nil` or no return value is specified then `target` will be returned.
@example
Pry.new.repl(Object.new)
Clean-up after the repl session. @param [Binding] target The target binding for the session.
Initialize the repl session. @param [Binding] target The target binding for the session.
Read and process a line of input — check for ^D, determine which prompt to use, rewrite the indentation if `Pry.config.auto_indent` is enabled, and, if the line is a command, process it and alter the eval_string accordingly. This method should not need to be invoked directly.
@param [String] eval_string The cumulative lines of input. @param [Binding] target The target of the session. @return [String] The line received.
Run the specified command. @param [String] val The command (and its params) to execute. @param [String] eval_string The current input buffer. @param [Binding] target The binding to use.. @return [Pry::Command::VOID_VALUE] @example
pry_instance.run_command("ls -m")
Set the last result of an eval. This method should not need to be invoked directly. @param [Object] result The result. @param [Binding] target The binding to set `_` on. @param [String] code The code that was run.
Whether the print proc should be invoked. Currently only invoked if the output is not suppressed OR the last result is an exception regardless of suppression. @return [Boolean] Whether the print proc should be invoked.
Update Pry‘s internal state after evalling code. This method should not need to be invoked directly. @param [String] code The code we just eval‘d