Class: Pry
- Extended by:
- Forwardable, Config::Convenience
- Defined in:
- lib/pry/pry_instance.rb,
lib/pry.rb,
lib/pry/cli.rb,
lib/pry/code.rb,
lib/pry/repl.rb,
lib/pry/hooks.rb,
lib/pry/editor.rb,
lib/pry/indent.rb,
lib/pry/method.rb,
lib/pry/output.rb,
lib/pry/command.rb,
lib/pry/history.rb,
lib/pry/plugins.rb,
lib/pry/rubygem.rb,
lib/pry/version.rb,
lib/pry/code/loc.rb,
lib/pry/rbx_path.rb,
lib/pry/pry_class.rb,
lib/pry/exceptions.rb,
lib/pry/input_lock.rb,
lib/pry/code_object.rb,
lib/pry/command_set.rb,
lib/pry/commands/cd.rb,
lib/pry/commands/ls.rb,
lib/pry/commands/ri.rb,
lib/pry/object_path.rb,
lib/pry/commands/cat.rb,
lib/pry/commands/wtf.rb,
lib/pry/helpers/text.rb,
lib/pry/color_printer.rb,
lib/pry/commands/bang.rb,
lib/pry/commands/edit.rb,
lib/pry/commands/exit.rb,
lib/pry/commands/gist.rb,
lib/pry/commands/help.rb,
lib/pry/commands/hist.rb,
lib/pry/commands/play.rb,
lib/pry/commands/stat.rb,
lib/pry/helpers/table.rb,
lib/pry/history_array.rb,
lib/pry/code/code_file.rb,
lib/pry/commands/reset.rb,
lib/pry/method/patcher.rb,
lib/pry/wrapped_module.rb,
lib/pry/code/code_range.rb,
lib/pry/commands/gem_cd.rb,
lib/pry/core_extensions.rb,
lib/pry/method/disowned.rb,
lib/pry/commands/jump_to.rb,
lib/pry/commands/ls/grep.rb,
lib/pry/commands/nesting.rb,
lib/pry/module_candidate.rb,
lib/pry/repl_file_loader.rb,
lib/pry/commands/bang_pry.rb,
lib/pry/commands/exit_all.rb,
lib/pry/commands/gem_list.rb,
lib/pry/commands/gem_open.rb,
lib/pry/commands/raise_up.rb,
lib/pry/commands/show_doc.rb,
lib/pry/commands/whereami.rb,
lib/pry/commands/save_file.rb,
lib/pry/commands/show_info.rb,
lib/pry/commands/switch_to.rb,
lib/pry/commands/amend_line.rb,
lib/pry/commands/fix_indent.rb,
lib/pry/commands/import_set.rb,
lib/pry/commands/ls/globals.rb,
lib/pry/commands/ls/methods.rb,
lib/pry/commands/shell_mode.rb,
lib/pry/commands/show_input.rb,
lib/pry/commands/disable_pry.rb,
lib/pry/commands/easter_eggs.rb,
lib/pry/commands/find_method.rb,
lib/pry/commands/gem_install.rb,
lib/pry/commands/pry_version.rb,
lib/pry/commands/reload_code.rb,
lib/pry/commands/show_source.rb,
lib/pry/helpers/base_helpers.rb,
lib/pry/commands/exit_program.rb,
lib/pry/commands/ls/constants.rb,
lib/pry/commands/ls/formatter.rb,
lib/pry/commands/ls/ls_entity.rb,
lib/pry/commands/toggle_color.rb,
lib/pry/commands/ls/local_vars.rb,
lib/pry/commands/pry_backtrace.rb,
lib/pry/commands/shell_command.rb,
lib/pry/commands/simple_prompt.rb,
lib/pry/commands/code_collector.rb,
lib/pry/commands/ls/local_names.rb,
lib/pry/helpers/command_helpers.rb,
lib/pry/helpers/options_helpers.rb,
lib/pry/commands/install_command.rb,
lib/pry/commands/ls/self_methods.rb,
lib/pry/commands/ls/instance_vars.rb,
lib/pry/commands/watch_expression.rb,
lib/pry/commands/cat/file_formatter.rb,
lib/pry/method/weird_method_locator.rb,
lib/pry/helpers/documentation_helpers.rb,
lib/pry/commands/cat/abstract_formatter.rb,
lib/pry/commands/edit/exception_patcher.rb,
lib/pry/commands/cat/exception_formatter.rb,
lib/pry/commands/edit/file_and_line_locator.rb,
lib/pry/commands/watch_expression/expression.rb,
lib/pry/commands/cat/input_expression_formatter.rb
Overview
PP subclass for streaming inspect output in color.
Defined Under Namespace
Modules: ExtendCommandBundle, FrozenObjectException, Helpers, RbxPath, RescuableException, Rubygem, TooSafeException, UserError Classes: BlockCommand, CLI, ClassCommand, Code, CodeFile, CodeObject, ColorPrinter, Command, CommandError, CommandSet, Config, Editor, History, HistoryArray, Hooks, Indent, InputCompleter, InputLock, Inspector, LastException, Method, MethodNotFound, NoCommandError, ObjectPath, ObsoleteError, Output, Pager, PluginManager, Prompt, REPL, REPLFileLoader, Result, Terminal, WrappedModule
Constant Summary collapse
- DEFAULT_HOOKS =
The default hooks - display messages when beginning and ending Pry sessions.
Pry::Hooks.new.add_hook(:before_session, :default) do |out, target, _pry_| next if _pry_.quiet? _pry_.run_command("whereami --quiet") end
- DEFAULT_PRINT =
The default print
proc do |output, value, _pry_| _pry_.pager.open do |pager| pager.print _pry_.config.output_prefix Pry::ColorPrinter.pp(value, pager, Pry::Terminal.width! - 1) end end
- SIMPLE_PRINT =
may be convenient when working with enormous objects and pretty_print is too slow
proc do |output, value| begin output.puts value.inspect rescue RescuableException output.puts "unknown" end end
- CLIPPED_PRINT =
useful when playing with truly enormous objects
proc do |output, value| output.puts Pry.view_clip(value, id: true) end
- DEFAULT_EXCEPTION_HANDLER =
Will only show the first line of the backtrace
proc do |output, exception, _| if UserError === exception && SyntaxError === exception output.puts "SyntaxError: #{exception..sub(/.*syntax error, */m, '')}" else output.puts "#{exception.class}: #{exception.}" output.puts "from #{exception.backtrace.first}" end end
- DEFAULT_PROMPT_NAME =
'pry'
- DEFAULT_PROMPT =
The default prompt; includes the target and nesting level
[ proc { |target_self, nest_level, pry| "[#{pry.input_array.size}] #{pry.config.prompt_name}(#{Pry.view_clip(target_self)})#{":#{nest_level}" unless nest_level.zero?}> " }, proc { |target_self, nest_level, pry| "[#{pry.input_array.size}] #{pry.config.prompt_name}(#{Pry.view_clip(target_self)})#{":#{nest_level}" unless nest_level.zero?}* " } ]
- DEFAULT_PROMPT_SAFE_OBJECTS =
[String, Numeric, Symbol, nil, true, false]
- SIMPLE_PROMPT =
A simple prompt - doesn’t display target or nesting level
[proc { ">> " }, proc { " | " }]
- NO_PROMPT =
[proc { '' }, proc { '' }]
- SHELL_PROMPT =
[ proc { |target_self, _, _pry_| "#{_pry_.config.prompt_name} #{Pry.view_clip(target_self)}:#{Dir.pwd} $ " }, proc { |target_self, _, _pry_| "#{_pry_.config.prompt_name} #{Pry.view_clip(target_self)}:#{Dir.pwd} * " } ]
- NAV_PROMPT =
A prompt that includes the full object path as well as input/output (in and out) information. Good for navigation.
[ proc do |_, _, _pry_| tree = _pry_.binding_stack.map { |b| Pry.view_clip(b.eval("self")) }.join " / " "[#{_pry_.input_array.count}] (#{_pry_.config.prompt_name}) #{tree}: #{_pry_.binding_stack.size - 1}> " end, proc do |_, _, _pry_| tree = _pry_.binding_stack.map { |b| Pry.view_clip(b.eval("self")) }.join " / " "[#{_pry_.input_array.count}] (#{ _pry_.config.prompt_name}) #{tree}: #{_pry_.binding_stack.size - 1}* " end, ]
- DEFAULT_CONTROL_D_HANDLER =
Deal with the ^D key being pressed. Different behaviour in different cases:
1. In an expression behave like `!` command. 2. At top-level session behave like `exit` command. 3. In a nested session behave like `cd ..`.
proc do |eval_string, _pry_| if !eval_string.empty? eval_string.replace('') # Clear input buffer. elsif _pry_.binding_stack.one? _pry_.binding_stack.clear throw(:breakout) else # Otherwise, saves current binding stack as old stack and pops last # binding out of binding stack (the old stack still has that binding). _pry_.command_state["cd"] ||= Pry::Config.from_hash({}) # FIXME _pry_.command_state['cd'].old_stack = _pry_.binding_stack.dup _pry_.binding_stack.pop end end
- DEFAULT_SYSTEM =
proc do |output, cmd, _| if !system(cmd) output.puts "Error: there was a problem executing system command: #{cmd}" end end
- INITIAL_PWD =
Store the current working directory. This allows show-source etc. to work if your process has changed directory since boot. [Issue #675]
Dir.pwd
- VERSION =
"0.10.3"
- Commands =
Default commands used by Pry.
Pry::CommandSet.new
- HOME_RC_FILE =
ENV["PRYRC"] || "~/.pryrc"
- LOCAL_RC_FILE =
"./.pryrc"
- DEFAULT_EXCEPTION_WHITELIST =
Don’t catch these exceptions
[SystemExit, SignalException, Pry::TooSafeException]
- EMPTY_COMPLETIONS =
[].freeze
- BINDING_METHOD_IMPL =
Returns Code of the method used when implementing Pry’s __binding__, along with line indication to be used with instance_eval (and friends).
[<<-METHOD, __FILE__, __LINE__ + 1] # Get a binding with 'self' set to self, and no locals. # # The default definee is determined by the context in which the # definition is eval'd. # # Please don't call this method directly, see {__binding__}. # # @return [Binding] def __pry__ binding end METHOD
Constants included from Config::Convenience
Config::Convenience::SHORTCUTS
Class Attribute Summary collapse
-
.cli ⇒ Object
Returns the value of attribute cli.
-
.config ⇒ Object
Returns the value of attribute config.
-
.current_line ⇒ Object
Returns the value of attribute current_line.
-
.custom_completions ⇒ Object
Returns the value of attribute custom_completions.
-
.eval_path ⇒ Object
Returns the value of attribute eval_path.
- .history ⇒ Object
-
.last_internal_error ⇒ Object
Returns the value of attribute last_internal_error.
-
.line_buffer ⇒ Object
Returns the value of attribute line_buffer.
-
.quiet ⇒ Object
Returns the value of attribute quiet.
Instance Attribute Summary collapse
-
#backtrace ⇒ Object
Returns the value of attribute backtrace.
-
#binding_stack ⇒ Object
Returns the value of attribute binding_stack.
-
#command_state ⇒ Object
readonly
Returns the value of attribute command_state.
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#custom_completions ⇒ Object
Returns the value of attribute custom_completions.
-
#eval_string ⇒ Object
Returns the value of attribute eval_string.
-
#exit_value ⇒ Object
readonly
Returns the value of attribute exit_value.
-
#input_array ⇒ Object
readonly
Returns the value of attribute input_array.
-
#last_dir ⇒ Object
Returns the value of attribute last_dir.
-
#last_exception ⇒ Object
Returns the value of attribute last_exception.
-
#last_file ⇒ Object
Returns the value of attribute last_file.
-
#last_result ⇒ Object
Returns the value of attribute last_result.
-
#output_array ⇒ Object
readonly
Returns the value of attribute output_array.
-
#suppress_output ⇒ Object
Returns the value of attribute suppress_output.
Class Method Summary collapse
- .auto_resize! ⇒ Object
-
.binding_for(target) ⇒ Binding
Return a ‘Binding` object for `target` or return `target` if it is already a `Binding`.
-
.Code(obj) ⇒ Object
Convert the given object into an instance of ‘Pry::Code`, if it isn’t already one.
- .critical_section(&block) ⇒ Object
-
.current ⇒ Pry::Config
Returns a value store for an instance of Pry running on the current thread.
- .default_editor_for_platform ⇒ Object
- .in_critical_section? ⇒ Boolean
-
.init ⇒ Object
Basic initialization.
-
.initial_session? ⇒ Boolean
Whether this is the first time a Pry session has been started since loading the Pry class.
-
.initial_session_setup ⇒ Object
Do basic setup for initial session.
-
.load_file_at_toplevel(file) ⇒ Object
Load the given file in the context of ‘Pry.toplevel_binding`.
-
.load_file_through_repl(file_name) ⇒ Object
Execute the file through the REPL loop, non-interactively.
-
.load_history ⇒ Object
Load Readline history if required.
-
.load_rc_files ⇒ Object
Load HOME_RC_FILE and LOCAL_RC_FILE if appropriate This method can also be used to reload the files if they have changed.
-
.load_requires ⇒ Object
Load any Ruby files specified with the -r flag on the command line.
-
.load_traps ⇒ Object
Trap interrupts on jruby, and make them behave like MRI so we can catch them.
- .load_win32console ⇒ Object
-
.main ⇒ main
Returns the special instance of Object, “main”.
-
.Method(obj) ⇒ Object
If the given object is a ‘Pry::Method`, return it unaltered.
-
.orig_reset_defaults ⇒ Object
Set all the configurable options back to their default values.
- .prompt ⇒ Object
- .prompt=(value) ⇒ Object
-
.rc_files_to_load ⇒ Object
Load the local RC file (./.pryrc).
-
.real_path_to(file) ⇒ Object
Expand a file to its canonical name (following symlinks as appropriate).
-
.reset_defaults ⇒ Object
Set all the configurable options back to their default values.
-
.run_command(command_string, options = {}) ⇒ Object
Run a Pry command from outside a session.
-
.start(target = nil, options = {}) ⇒ Object
Start a Pry REPL.
- .toplevel_binding ⇒ Object
- .toplevel_binding=(binding) ⇒ Object
-
.view_clip(obj, options = {}) ⇒ String
An inspector that clips the output to ‘max_length` chars.
-
.WrappedModule(obj) ⇒ Object
If the given object is a ‘Pry::WrappedModule`, return it unaltered.
Instance Method Summary collapse
-
#add_sticky_local(name) { ... } ⇒ Object
Add a sticky local to this Pry instance.
-
#complete(str) ⇒ Array<String>
Generate completions.
-
#current_binding ⇒ Binding
(also: #current_context)
The currently active ‘Binding`.
-
#eval(line, options = {}) ⇒ Boolean
Pass a line of input to Pry.
- #evaluate_ruby(code) ⇒ Object
-
#exec_hook(name, *args, &block) ⇒ Object, Exception
Execute the specified hook.
-
#initialize(options = {}) ⇒ Pry
constructor
Create a new Pry instance.
-
#inject_local(name, value, b) ⇒ Object
Injects a local variable into the provided binding.
-
#inject_sticky_locals! ⇒ Object
Inject all the sticky locals into the current binding.
-
#last_result_is_exception? ⇒ 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).
-
#memory_size ⇒ Integer
The maximum amount of objects remembered by the inp and out arrays.
- #memory_size=(size) ⇒ Object
-
#output ⇒ Object
Returns an output device.
-
#pager ⇒ Object
Returns the currently configured pager.
-
#pop_prompt ⇒ Array<Proc>
Pops the current prompt off of the prompt stack.
-
#process_command(val) ⇒ Boolean
If the given line is a valid command, process it in the context of the current ‘eval_string` and binding.
-
#process_command_safely(val) ⇒ Boolean
Same as process_command, but outputs exceptions to ‘#output` instead of raising.
-
#prompt ⇒ Array<Proc>
The current prompt.
- #prompt=(new_prompt) ⇒ Object
-
#push_binding(object) ⇒ Object
Push a binding for the given object onto the stack.
-
#push_initial_binding(target = nil) ⇒ Object
Initialize this instance by pushing its initial context into the binding stack.
-
#push_prompt(new_prompt) ⇒ Array<Proc>
Pushes the current prompt onto a stack that it can be restored from later.
-
#quiet? ⇒ Boolean
Convenience accessor for the ‘quiet` config key.
- #raise_up(*args) ⇒ Object
- #raise_up!(*args) ⇒ Object
-
#raise_up_common(force, *args) ⇒ Object
Raise an exception out of Pry.
-
#repl(target = nil) ⇒ Object
Potentially deprecated — Use ‘Pry::REPL.new(pry, :target => target).start` (If nested sessions are going to exist, this method is fine, but a goal is to come up with an alternative to nested sessions altogether.).
-
#reset_eval_string ⇒ Object
Reset the current eval string.
-
#run_command(val) ⇒ Pry::Command::VOID_VALUE
Run the specified command.
-
#select_prompt ⇒ String
Returns the appropriate prompt to use.
-
#set_last_result(result, code = "") ⇒ Object
Set the last result of an eval.
-
#should_print? ⇒ Boolean
Whether the print proc should be invoked.
-
#show_result(result) ⇒ Object
Output the result or pass to an exception handler (if result is an exception).
- #sticky_locals ⇒ Object
-
#update_input_history(code) ⇒ Object
Update Pry’s internal state after evalling code.
Methods included from Config::Convenience
Constructor Details
#initialize(options = {}) ⇒ Pry
Create a new Pry instance.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/pry/pry_instance.rb', line 65 def initialize(={}) @binding_stack = [] @indent = Pry::Indent.new @command_state = {} @eval_string = "" @backtrace = .delete(:backtrace) || caller target = .delete(:target) @config = Pry::Config.new config.merge!() push_prompt(config.prompt) @input_array = Pry::HistoryArray.new config.memory_size @output_array = Pry::HistoryArray.new config.memory_size @custom_completions = config.command_completions set_last_result nil @input_array << nil push_initial_binding(target) exec_hook(:when_started, target, , self) end |
Class Attribute Details
.cli ⇒ Object
Returns the value of attribute cli.
13 14 15 |
# File 'lib/pry/pry_class.rb', line 13 def cli @cli end |
.config ⇒ Object
Returns the value of attribute config.
16 17 18 |
# File 'lib/pry/pry_class.rb', line 16 def config @config end |
.current_line ⇒ Object
Returns the value of attribute current_line.
10 11 12 |
# File 'lib/pry/pry_class.rb', line 10 def current_line @current_line end |
.custom_completions ⇒ Object
Returns the value of attribute custom_completions.
9 10 11 |
# File 'lib/pry/pry_class.rb', line 9 def custom_completions @custom_completions end |
.eval_path ⇒ Object
Returns the value of attribute eval_path.
12 13 14 |
# File 'lib/pry/pry_class.rb', line 12 def eval_path @eval_path end |
.history ⇒ Object
32 33 34 |
# File 'lib/pry/pry_class.rb', line 32 def history @history ||= History.new end |
.last_internal_error ⇒ Object
Returns the value of attribute last_internal_error.
15 16 17 |
# File 'lib/pry/pry_class.rb', line 15 def last_internal_error @last_internal_error end |
.line_buffer ⇒ Object
Returns the value of attribute line_buffer.
11 12 13 |
# File 'lib/pry/pry_class.rb', line 11 def line_buffer @line_buffer end |
.quiet ⇒ Object
Returns the value of attribute quiet.
14 15 16 |
# File 'lib/pry/pry_class.rb', line 14 def quiet @quiet end |
Instance Attribute Details
#backtrace ⇒ Object
Returns the value of attribute backtrace.
28 29 30 |
# File 'lib/pry/pry_instance.rb', line 28 def backtrace @backtrace end |
#binding_stack ⇒ Object
Returns the value of attribute binding_stack.
25 26 27 |
# File 'lib/pry/pry_instance.rb', line 25 def binding_stack @binding_stack end |
#command_state ⇒ Object (readonly)
Returns the value of attribute command_state.
35 36 37 |
# File 'lib/pry/pry_instance.rb', line 35 def command_state @command_state end |
#config ⇒ Object (readonly)
Returns the value of attribute config.
39 40 41 |
# File 'lib/pry/pry_instance.rb', line 39 def config @config end |
#custom_completions ⇒ Object
Returns the value of attribute custom_completions.
26 27 28 |
# File 'lib/pry/pry_instance.rb', line 26 def custom_completions @custom_completions end |
#eval_string ⇒ Object
Returns the value of attribute eval_string.
27 28 29 |
# File 'lib/pry/pry_instance.rb', line 27 def eval_string @eval_string end |
#exit_value ⇒ Object (readonly)
Returns the value of attribute exit_value.
36 37 38 |
# File 'lib/pry/pry_instance.rb', line 36 def exit_value @exit_value end |
#input_array ⇒ Object (readonly)
Returns the value of attribute input_array.
37 38 39 |
# File 'lib/pry/pry_instance.rb', line 37 def input_array @input_array end |
#last_dir ⇒ Object
Returns the value of attribute last_dir.
32 33 34 |
# File 'lib/pry/pry_instance.rb', line 32 def last_dir @last_dir end |
#last_exception ⇒ Object
Returns the value of attribute last_exception.
34 35 36 |
# File 'lib/pry/pry_instance.rb', line 34 def last_exception @last_exception end |
#last_file ⇒ Object
Returns the value of attribute last_file.
31 32 33 |
# File 'lib/pry/pry_instance.rb', line 31 def last_file @last_file end |
#last_result ⇒ Object
Returns the value of attribute last_result.
30 31 32 |
# File 'lib/pry/pry_instance.rb', line 30 def last_result @last_result end |
#output_array ⇒ Object (readonly)
Returns the value of attribute output_array.
38 39 40 |
# File 'lib/pry/pry_instance.rb', line 38 def output_array @output_array end |
#suppress_output ⇒ Object
Returns the value of attribute suppress_output.
29 30 31 |
# File 'lib/pry/pry_instance.rb', line 29 def suppress_output @suppress_output end |
Class Method Details
.auto_resize! ⇒ Object
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
# File 'lib/pry/pry_class.rb', line 275 def self.auto_resize! Pry.config.input # by default, load Readline if !defined?(Readline) || Pry.config.input != Readline warn "Sorry, you must be using Readline for Pry.auto_resize! to work." return end if Readline::VERSION =~ /edit/i warn <<-EOT Readline version #{Readline::VERSION} detected - will not auto_resize! correctly. For the fix, use GNU Readline instead: https://github.com/guard/guard/wiki/Add-proper-Readline-support-to-Ruby-on-Mac-OS-X EOT return end trap :WINCH do begin Readline.set_screen_size(*Terminal.size!) rescue => e warn "\nPry.auto_resize!'s Readline.set_screen_size failed: #{e}" end begin Readline.refresh_line rescue => e warn "\nPry.auto_resize!'s Readline.refresh_line failed: #{e}" end end end |
.binding_for(target) ⇒ Binding
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`
328 329 330 331 332 333 334 335 336 337 338 |
# File 'lib/pry/pry_class.rb', line 328 def self.binding_for(target) if Binding === target target else if Pry.main == target TOPLEVEL_BINDING else target.__binding__ end end end |
.Code(obj) ⇒ Object
Convert the given object into an instance of ‘Pry::Code`, if it isn’t already one.
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/pry/code.rb', line 12 def Code(obj) case obj when Code obj when ::Method, UnboundMethod, Proc, Pry::Method Code.from_method(obj) else Code.new(obj) end end |
.critical_section(&block) ⇒ Object
366 367 368 369 370 371 372 |
# File 'lib/pry/pry_class.rb', line 366 def self.critical_section(&block) Thread.current[:pry_critical_section] ||= 0 Thread.current[:pry_critical_section] += 1 yield ensure Thread.current[:pry_critical_section] -= 1 end |
.current ⇒ Pry::Config
Returns a value store for an instance of Pry running on the current thread.
49 50 51 |
# File 'lib/pry/pry_class.rb', line 49 def self.current Thread.current[:__pry__] ||= Pry::Config.from_hash({}, nil) end |
.default_editor_for_platform ⇒ Object
263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/pry/pry_class.rb', line 263 def self.default_editor_for_platform return ENV['VISUAL'] if ENV['VISUAL'] and not ENV['VISUAL'].empty? return ENV['EDITOR'] if ENV['EDITOR'] and not ENV['EDITOR'].empty? if Helpers::BaseHelpers.windows? 'notepad' else %w(editor nano vi).detect do |editor| system("which #{editor} > /dev/null 2>&1") end end end |
.in_critical_section? ⇒ Boolean
361 362 363 364 |
# File 'lib/pry/pry_class.rb', line 361 def self.in_critical_section? Thread.current[:pry_critical_section] ||= 0 Thread.current[:pry_critical_section] > 0 end |
.init ⇒ Object
Basic initialization.
317 318 319 320 321 |
# File 'lib/pry/pry_class.rb', line 317 def self.init @plugin_manager ||= PluginManager.new reset_defaults locate_plugins end |
.initial_session? ⇒ Boolean
Returns Whether this is the first time a Pry session has been started since loading the Pry class.
227 228 229 |
# File 'lib/pry/pry_class.rb', line 227 def self.initial_session? @initial_session end |
.initial_session_setup ⇒ Object
Do basic setup for initial session. Including: loading .pryrc, loading plugins, loading requires, and loading history.
120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/pry/pry_class.rb', line 120 def self.initial_session_setup return unless initial_session? @initial_session = false # note these have to be loaded here rather than in pry_instance as # we only want them loaded once per entire Pry lifetime. load_rc_files load_plugins if Pry.config.should_load_plugins load_requires if Pry.config.should_load_requires load_history if Pry.config.history.should_load load_traps if Pry.config.should_trap_interrupts load_win32console if Pry::Helpers::BaseHelpers.windows? && !Pry::Helpers::BaseHelpers.windows_ansi? end |
.load_file_at_toplevel(file) ⇒ Object
Load the given file in the context of ‘Pry.toplevel_binding`
55 56 57 58 59 |
# File 'lib/pry/pry_class.rb', line 55 def self.load_file_at_toplevel(file) toplevel_binding.eval(File.read(file), file) rescue RescuableException => e puts "Error loading #{file}: #{e}\n#{e.backtrace.first}" end |
.load_file_through_repl(file_name) ⇒ Object
Execute the file through the REPL loop, non-interactively.
177 178 179 180 |
# File 'lib/pry/pry_class.rb', line 177 def self.load_file_through_repl(file_name) require "pry/repl_file_loader" REPLFileLoader.new(file_name).load end |
.load_history ⇒ Object
Load Readline history if required.
221 222 223 |
# File 'lib/pry/pry_class.rb', line 221 def self.load_history Pry.history.load end |
.load_rc_files ⇒ Object
Load HOME_RC_FILE and LOCAL_RC_FILE if appropriate This method can also be used to reload the files if they have changed.
63 64 65 66 67 68 69 |
# File 'lib/pry/pry_class.rb', line 63 def self.load_rc_files rc_files_to_load.each do |file| critical_section do load_file_at_toplevel(file) end end end |
.load_requires ⇒ Object
Load any Ruby files specified with the -r flag on the command line.
89 90 91 92 93 |
# File 'lib/pry/pry_class.rb', line 89 def self.load_requires Pry.config.requires.each do |file| require file end end |
.load_traps ⇒ Object
Trap interrupts on jruby, and make them behave like MRI so we can catch them.
97 98 99 |
# File 'lib/pry/pry_class.rb', line 97 def self.load_traps trap('INT'){ raise Interrupt } end |
.load_win32console ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/pry/pry_class.rb', line 101 def self.load_win32console begin require 'win32console' # The mswin and mingw versions of pry require win32console, so this should # only fail on jruby (where win32console doesn't work). # Instead we'll recommend ansicon, which does. rescue LoadError warn <<-WARNING if Pry.config.windows_console_warning For a better Pry experience on Windows, please use ansicon: https://github.com/adoxa/ansicon If you use an alternative to ansicon and don't want to see this warning again, you can add "Pry.config.windows_console_warning = false" to your .pryrc. WARNING end end |
.main ⇒ main
Returns the special instance of Object, “main”.
41 42 43 |
# File 'lib/pry/pry_class.rb', line 41 def self.main @main ||= TOPLEVEL_BINDING.eval "self" end |
.Method(obj) ⇒ Object
If the given object is a ‘Pry::Method`, return it unaltered. If it’s anything else, return it wrapped in a ‘Pry::Method` instance.
7 8 9 10 11 12 13 |
# File 'lib/pry/method.rb', line 7 def Method(obj) if obj.is_a? Pry::Method obj else Pry::Method.new(obj) end end |
.orig_reset_defaults ⇒ Object
Set all the configurable options back to their default values
6 7 8 9 10 11 12 13 |
# File 'lib/pry/test/helper.rb', line 6 def self.reset_defaults @initial_session = true self.config = Pry::Config.new Pry::Config::Default.new self.cli = false self.current_line = 1 self.line_buffer = [""] self.eval_path = "(pry)" end |
.prompt ⇒ Object
28 29 30 |
# File 'lib/pry/pry_class.rb', line 28 def prompt config.prompt end |
.prompt=(value) ⇒ Object
24 25 26 |
# File 'lib/pry/pry_class.rb', line 24 def prompt=(value) config.prompt = value end |
.rc_files_to_load ⇒ Object
Load the local RC file (./.pryrc)
72 73 74 75 76 77 |
# File 'lib/pry/pry_class.rb', line 72 def self.rc_files_to_load files = [] files << HOME_RC_FILE if Pry.config.should_load_rc files << LOCAL_RC_FILE if Pry.config.should_load_local_rc files.map { |file| real_path_to(file) }.compact.uniq end |
.real_path_to(file) ⇒ Object
Expand a file to its canonical name (following symlinks as appropriate)
80 81 82 83 84 85 86 |
# File 'lib/pry/pry_class.rb', line 80 def self.real_path_to(file) = Pathname.new(File.(file)).realpath.to_s # For rbx 1.9 mode [see rubinius issue #2165] File.exist?() ? : nil rescue Errno::ENOENT nil end |
.reset_defaults ⇒ Object
Set all the configurable options back to their default values
307 308 309 310 311 312 313 314 |
# File 'lib/pry/pry_class.rb', line 307 def self.reset_defaults @initial_session = true self.config = Pry::Config.new Pry::Config::Default.new self.cli = false self.current_line = 1 self.line_buffer = [""] self.eval_path = "(pry)" end |
.run_command(command_string, options = {}) ⇒ Object
Run a Pry command from outside a session. The commands available are those referenced by ‘Pry.config.commands` (the default command set).
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
# File 'lib/pry/pry_class.rb', line 247 def self.run_command(command_string, ={}) = { :target => TOPLEVEL_BINDING, :show_output => true, :output => Pry.config.output, :commands => Pry.config.commands }.merge!() # :context for compatibility with <= 0.9.11.4 target = [:context] || [:target] output = [:show_output] ? [:output] : StringIO.new pry = Pry.new(:output => output, :target => target, :commands => [:commands]) pry.eval command_string end |
.start(target = nil, options = {}) ⇒ Object
Start a Pry REPL. This method also loads ‘~/.pryrc` and `./.pryrc` as necessary the first time it is invoked.
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/pry/pry_class.rb', line 142 def self.start(target=nil, ={}) return if ENV['DISABLE_PRY'] = .to_hash if in_critical_section? output.puts "ERROR: Pry started inside Pry." output.puts "This can happen if you have a binding.pry inside a #to_s or #inspect function." return end [:target] = Pry.binding_for(target || toplevel_binding) [:hooks] = Pry::Hooks.from_hash .delete(:hooks) if .key?(:hooks) initial_session_setup # Unless we were given a backtrace, save the current one if [:backtrace].nil? [:backtrace] = caller # If Pry was started via `binding.pry`, elide that from the backtrace if [:backtrace].first =~ /pry.*core_extensions.*pry/ [:backtrace].shift end end driver = [:driver] || Pry::REPL # Enter the matrix driver.start() rescue Pry::TooSafeException puts "ERROR: Pry cannot work with $SAFE > 0" raise end |
.toplevel_binding ⇒ Object
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 |
# File 'lib/pry/pry_class.rb', line 340 def self.toplevel_binding unless defined?(@toplevel_binding) && @toplevel_binding # Grab a copy of the TOPLEVEL_BINDING without any local variables. # This binding has a default definee of Object, and new methods are # private (just as in TOPLEVEL_BINDING). TOPLEVEL_BINDING.eval <<-RUBY def self.__pry__ binding end Pry.toplevel_binding = __pry__ class << self; undef __pry__; end RUBY end @toplevel_binding.eval('private') @toplevel_binding end |
.toplevel_binding=(binding) ⇒ Object
357 358 359 |
# File 'lib/pry/pry_class.rb', line 357 def self.toplevel_binding=(binding) @toplevel_binding = binding end |
.view_clip(obj, options = {}) ⇒ String
An inspector that clips the output to ‘max_length` chars. In case of > `max_length` chars the `#<Object…> notation is used.
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/pry/pry_class.rb', line 201 def self.view_clip(obj, = {}) max = .fetch :max_length, 60 id = .fetch :id, false if obj.kind_of?(Module) && obj.name.to_s != "" && obj.name.to_s.length <= max obj.name.to_s elsif Pry.main == obj # special-case to support jruby. # fixed as of https://github.com/jruby/jruby/commit/d365ebd309cf9df3dde28f5eb36ea97056e0c039 # we can drop in the future. obj.to_s elsif Pry.config.prompt_safe_objects.any? { |v| v === obj } && obj.inspect.length <= max obj.inspect else id == true ? "#<#{obj.class}:0x%x>" % (obj.object_id << 1) : "#<#{obj.class}>" end rescue RescuableException "unknown" end |
.WrappedModule(obj) ⇒ Object
If the given object is a ‘Pry::WrappedModule`, return it unaltered. If it’s anything else, return it wrapped in a ‘Pry::WrappedModule` instance.
7 8 9 10 11 12 13 |
# File 'lib/pry/wrapped_module.rb', line 7 def WrappedModule(obj) if obj.is_a? Pry::WrappedModule obj else Pry::WrappedModule.new(obj) end end |
Instance Method Details
#add_sticky_local(name) { ... } ⇒ Object
Add a sticky local to this Pry instance. A sticky local is a local that persists between all bindings in a session.
195 196 197 |
# File 'lib/pry/pry_instance.rb', line 195 def add_sticky_local(name, &block) config.extra_sticky_locals[name] = block end |
#complete(str) ⇒ Array<String>
Generate completions.
133 134 135 136 137 138 139 |
# File 'lib/pry/pry_instance.rb', line 133 def complete(str) return EMPTY_COMPLETIONS unless config.completer Pry.critical_section do completer = config.completer.new(config.input, self) completer.call str, target: current_binding, custom_completions: custom_completions.call.push(*sticky_locals.keys) end end |
#current_binding ⇒ Binding Also known as: current_context
The currently active ‘Binding`.
112 113 114 |
# File 'lib/pry/pry_instance.rb', line 112 def current_binding binding_stack.last end |
#eval(line, options = {}) ⇒ Boolean
Pass a line of input to Pry.
This is the equivalent of ‘Binding#eval` but with extra Pry!
In particular:
-
Pry commands will be executed immediately if the line matches.
-
Partial lines of input will be queued up until a complete expression has been accepted.
-
Output is written to ‘#output` in pretty colours, not returned.
Once this method has raised an exception or returned false, this instance is no longer usable. #exit_value will return the session’s breakout value if applicable.
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/pry/pry_instance.rb', line 237 def eval(line, ={}) return false if @stopped exit_value = nil exception = catch(:raise_up) do exit_value = catch(:breakout) do handle_line(line, ) # We use 'return !@stopped' here instead of 'return true' so that if # handle_line has stopped this pry instance (e.g. by opening _pry_.repl and # then popping all the bindings) we still exit immediately. return !@stopped end exception = false end @stopped = true @exit_value = exit_value # TODO: make this configurable? raise exception if exception return false end |
#evaluate_ruby(code) ⇒ Object
351 352 353 354 355 356 357 358 359 360 |
# File 'lib/pry/pry_instance.rb', line 351 def evaluate_ruby(code) inject_sticky_locals! exec_hook :before_eval, code, self result = current_binding.eval(code, Pry.eval_path, Pry.current_line) set_last_result(result, code) ensure update_input_history(code) exec_hook :after_eval, result, self end |
#exec_hook(name, *args, &block) ⇒ Object, Exception
Execute the specified hook. 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.
465 466 467 468 469 470 471 472 473 474 |
# File 'lib/pry/pry_instance.rb', line 465 def exec_hook(name, *args, &block) e_before = hooks.errors.size hooks.exec_hook(name, *args, &block).tap do hooks.errors[e_before..-1].each do |e| output.puts "#{name} hook failed: #{e.class}: #{e.}" output.puts "#{e.backtrace.first}" output.puts "(see _pry_.hooks.errors to debug)" end end end |
#inject_local(name, value, b) ⇒ Object
Injects a local variable into the provided binding.
156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/pry/pry_instance.rb', line 156 def inject_local(name, value, b) value = Proc === value ? value.call : value if b.respond_to?(:local_variable_set) b.local_variable_set name, value else # < 2.1 begin Pry.current[:pry_local] = value b.eval "#{name} = ::Pry.current[:pry_local]" ensure Pry.current[:pry_local] = nil end end end |
#inject_sticky_locals! ⇒ Object
Inject all the sticky locals into the current binding.
184 185 186 187 188 |
# File 'lib/pry/pry_instance.rb', line 184 def inject_sticky_locals! sticky_locals.each_pair do |name, value| inject_local(name, value, current_binding) end end |
#last_result_is_exception? ⇒ Boolean
Returns 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).
515 516 517 |
# File 'lib/pry/pry_instance.rb', line 515 def last_result_is_exception? @last_result_is_exception end |
#memory_size ⇒ Integer
Returns The maximum amount of objects remembered by the inp and out arrays. Defaults to 100.
173 174 175 |
# File 'lib/pry/pry_instance.rb', line 173 def memory_size @output_array.max_size end |
#memory_size=(size) ⇒ Object
178 179 180 181 |
# File 'lib/pry/pry_instance.rb', line 178 def memory_size=(size) @input_array = Pry::HistoryArray.new(size) @output_array = Pry::HistoryArray.new(size) end |
#output ⇒ Object
Returns an output device
613 614 615 |
# File 'lib/pry/pry_instance.rb', line 613 def output Pry::Output.new(self) end |
#pager ⇒ Object
Returns the currently configured pager
605 606 607 |
# File 'lib/pry/pry_instance.rb', line 605 def pager Pry::Pager.new(self) end |
#pop_prompt ⇒ Array<Proc>
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.
597 598 599 |
# File 'lib/pry/pry_instance.rb', line 597 def pop_prompt prompt_stack.size > 1 ? prompt_stack.pop : prompt end |
#process_command(val) ⇒ Boolean
If the given line is a valid command, process it in the context of the current ‘eval_string` and binding.
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 |
# File 'lib/pry/pry_instance.rb', line 402 def process_command(val) val = val.chomp result = commands.process_line(val, :target => current_binding, :output => output, :eval_string => @eval_string, :pry_instance => self ) # set a temporary (just so we can inject the value we want into eval_string) Pry.current[:pry_cmd_result] = result # note that `result` wraps the result of command processing; if a # command was matched and invoked then `result.command?` returns true, # otherwise it returns false. if result.command? if !result.void_command? # the command that was invoked was non-void (had a return value) and so we make # the value of the current expression equal to the return value # of the command. @eval_string.replace "::Pry.current[:pry_cmd_result].retval\n" end true else false end end |
#process_command_safely(val) ⇒ Boolean
Same as process_command, but outputs exceptions to ‘#output` instead of raising.
434 435 436 437 438 439 440 |
# File 'lib/pry/pry_instance.rb', line 434 def process_command_safely(val) process_command(val) rescue CommandError, Slop::InvalidOptionError, MethodSource::SourceNotFoundError => e Pry.last_internal_error = e output.puts "Error: #{e.}" true end |
#prompt ⇒ Array<Proc>
The current prompt. This is the prompt at the top of the prompt stack.
92 93 94 |
# File 'lib/pry/pry_instance.rb', line 92 def prompt prompt_stack.last end |
#prompt=(new_prompt) ⇒ Object
96 97 98 99 100 101 102 |
# File 'lib/pry/pry_instance.rb', line 96 def prompt=(new_prompt) if prompt_stack.empty? push_prompt new_prompt else prompt_stack[-1] = new_prompt end end |
#push_binding(object) ⇒ Object
Push a binding for the given object onto the stack. If this instance is currently stopped, mark it as usable again.
119 120 121 122 |
# File 'lib/pry/pry_instance.rb', line 119 def push_binding(object) @stopped = false binding_stack << Pry.binding_for(object) end |
#push_initial_binding(target = nil) ⇒ Object
Initialize this instance by pushing its initial context into the binding stack. If no target is given, start at the top level.
106 107 108 |
# File 'lib/pry/pry_instance.rb', line 106 def push_initial_binding(target=nil) push_binding(target || Pry.toplevel_binding) end |
#push_prompt(new_prompt) ⇒ Array<Proc>
Pushes the current prompt onto a stack that it can be restored from later. Use this if you wish to temporarily change the prompt.
581 582 583 |
# File 'lib/pry/pry_instance.rb', line 581 def push_prompt(new_prompt) prompt_stack.push new_prompt end |
#quiet? ⇒ Boolean
Convenience accessor for the ‘quiet` config key.
661 662 663 |
# File 'lib/pry/pry_instance.rb', line 661 def quiet? config.quiet end |
#raise_up(*args) ⇒ Object
656 |
# File 'lib/pry/pry_instance.rb', line 656 def raise_up(*args); raise_up_common(false, *args); end |
#raise_up!(*args) ⇒ Object
657 |
# File 'lib/pry/pry_instance.rb', line 657 def raise_up!(*args); raise_up_common(true, *args); end |
#raise_up_common(force, *args) ⇒ Object
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")
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 |
# File 'lib/pry/pry_instance.rb', line 629 def raise_up_common(force, *args) exception = if args == [] last_exception || RuntimeError.new elsif args.length == 1 && args.first.is_a?(String) RuntimeError.new(args.first) elsif args.length > 3 raise ArgumentError, "wrong number of arguments" elsif !args.first.respond_to?(:exception) raise TypeError, "exception class/object expected" elsif args.length === 1 args.first.exception else args.first.exception(args[1]) end raise TypeError, "exception object expected" unless exception.is_a? Exception exception.set_backtrace(args.length === 3 ? args[2] : caller(1)) if force || binding_stack.one? binding_stack.clear throw :raise_up, exception else binding_stack.pop raise exception end end |
#repl(target = nil) ⇒ Object
Potentially deprecated — Use ‘Pry::REPL.new(pry, :target => target).start` (If nested sessions are going to exist, this method is fine, but a goal is to come up with an alternative to nested sessions altogether.)
347 348 349 |
# File 'lib/pry/pry_instance.rb', line 347 def repl(target = nil) Pry::REPL.new(self, :target => target).start end |
#reset_eval_string ⇒ Object
Reset the current eval string. If the user has entered part of a multiline expression, this discards that input.
213 214 215 |
# File 'lib/pry/pry_instance.rb', line 213 def reset_eval_string @eval_string = "" end |
#run_command(val) ⇒ Pry::Command::VOID_VALUE
Run the specified command.
447 448 449 450 451 452 453 454 455 |
# File 'lib/pry/pry_instance.rb', line 447 def run_command(val) commands.process_line(val, :eval_string => @eval_string, :target => current_binding, :pry_instance => self, :output => output ) Pry::Command::VOID_VALUE end |
#select_prompt ⇒ String
Returns the appropriate prompt to use.
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 |
# File 'lib/pry/pry_instance.rb', line 528 def select_prompt object = current_binding.eval('self') open_token = @indent.open_delimiters.any? ? @indent.open_delimiters.last : @indent.stack.last c = Pry::Config.from_hash({ :object => object, :nesting_level => binding_stack.size - 1, :open_token => open_token, :session_line => Pry.history.session_line_count + 1, :history_line => Pry.history.history_line_count + 1, :expr_number => input_array.count, :_pry_ => self, :binding_stack => binding_stack, :input_array => input_array, :eval_string => @eval_string, :cont => !@eval_string.empty?}) Pry.critical_section do # If input buffer is empty then use normal prompt if eval_string.empty? generate_prompt(Array(prompt).first, c) # Otherwise use the wait prompt (indicating multi-line expression) else generate_prompt(Array(prompt).last, c) end end end |
#set_last_result(result, code = "") ⇒ Object
Set the last result of an eval. This method should not need to be invoked directly.
480 481 482 483 484 485 |
# File 'lib/pry/pry_instance.rb', line 480 def set_last_result(result, code="") @last_result_is_exception = false @output_array << result self.last_result = result unless code =~ /\A\s*\z/ end |
#should_print? ⇒ Boolean
Whether the print proc should be invoked. Currently only invoked if the output is not suppressed.
522 523 524 |
# File 'lib/pry/pry_instance.rb', line 522 def should_print? !@suppress_output end |
#show_result(result) ⇒ Object
Output the result or pass to an exception handler (if result is an exception).
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 |
# File 'lib/pry/pry_instance.rb', line 363 def show_result(result) if last_result_is_exception? exception_handler.call(output, result, self) elsif should_print? print.call(output, result, self) else # nothin' end rescue RescuableException => e # Being uber-paranoid here, given that this exception arose because we couldn't # serialize something in the user's program, let's not assume we can serialize # the exception either. begin output.puts "(pry) output error: #{e.inspect}" rescue RescuableException => e if last_result_is_exception? output.puts "(pry) output error: failed to show exception" else output.puts "(pry) output error: failed to show result" end end ensure output.flush if output.respond_to?(:flush) end |
#sticky_locals ⇒ Object
199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/pry/pry_instance.rb', line 199 def sticky_locals { _in_: input_array, _out_: output_array, _pry_: self, _ex_: last_exception && last_exception.wrapped_exception, _file_: last_file, _dir_: last_dir, _: proc { last_result }, __: proc { output_array[-2] } }.merge(config.extra_sticky_locals) end |
#update_input_history(code) ⇒ Object
Update Pry’s internal state after evalling code. This method should not need to be invoked directly.
503 504 505 506 507 508 509 510 |
# File 'lib/pry/pry_instance.rb', line 503 def update_input_history(code) # Always push to the @input_array as the @output_array is always pushed to. @input_array << code if code Pry.line_buffer.push(*code.each_line) Pry.current_line += code.lines.count end end |