Class: Pry
- Inherits:
-
Object
- Object
- Pry
- Extended by:
- Forwardable
- Defined in:
- lib/pry.rb,
lib/pry/cli.rb,
lib/pry/code.rb,
lib/pry/hooks.rb,
lib/pry/method.rb,
lib/pry/config.rb,
lib/pry/indent.rb,
lib/pry/history.rb,
lib/pry/version.rb,
lib/pry/command.rb,
lib/pry/plugins.rb,
lib/pry/rbx_path.rb,
lib/pry/commands.rb,
lib/pry/pry_class.rb,
lib/pry/completion.rb,
lib/pry/rbx_method.rb,
lib/pry/command_set.rb,
lib/pry/pry_instance.rb,
lib/pry/helpers/text.rb,
lib/pry/history_array.rb,
lib/pry/wrapped_module.rb,
lib/pry/custom_completions.rb,
lib/pry/default_commands/ls.rb,
lib/pry/default_commands/cd.rb,
lib/pry/helpers/base_helpers.rb,
lib/pry/default_commands/gems.rb,
lib/pry/default_commands/help.rb,
lib/pry/default_commands/hist.rb,
lib/pry/default_commands/misc.rb,
lib/pry/helpers/options_helpers.rb,
lib/pry/helpers/command_helpers.rb,
lib/pry/default_commands/editing.rb,
lib/pry/default_commands/context.rb,
lib/pry/default_commands/commands.rb,
lib/pry/default_commands/easter_eggs.rb,
lib/pry/default_commands/find_method.rb,
lib/pry/helpers/documentation_helpers.rb,
lib/pry/extended_commands/experimental.rb,
lib/pry/default_commands/introspection.rb,
lib/pry/default_commands/navigating_pry.rb,
lib/pry/default_commands/input_and_output.rb
Defined Under Namespace
Modules: DefaultCommands, ExtendCommandBundle, ExtendedCommands, Helpers, InputCompleter, RbxMethod, RbxPath, RescuableException Classes: BlockCommand, CLI, ClassCommand, Code, Command, CommandError, CommandSet, Config, History, HistoryArray, Hooks, Indent, Method, NoCommandError, NonMethodContextError, ObsoleteError, PluginManager, Result, WrappedModule
Constant Summary
- 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? # ensure we're actually in a method file = target.eval('__FILE__') # /unknown/ for rbx if file !~ /(\(.*\))|<.*>/ && file !~ /__unknown__/ && file != "" && file != "-e" || file == Pry.eval_path _pry_.run_command("whereami", "", target) end end
- DEFAULT_PRINT =
The default print
proc do |output, value| stringified = begin value.pretty_inspect rescue RescuableException nil end unless String === stringified # Read the class name off of the singleton class to provide a default inspect. klass = (class << value; self; end).ancestors.first stringified = "#<#{klass}:0x#{value.__id__.to_s(16)}>" end nonce = rand(0x100000000).to_s(16) # whatever colorized = Helpers::BaseHelpers.colorize_code(stringified.gsub(/#</, "%<#{nonce}")) # avoid colour-leak from CodeRay and any of the users' previous output colorized = colorized.sub(/(\n*)$/, "\e[0m\\1") if Pry.color Helpers::BaseHelpers.stagger_output("=> #{colorized.gsub(/%<(.*?)#{nonce}/, '#<\1')}", output) 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)}" end
- DEFAULT_EXCEPTION_HANDLER =
Will only show the first line of the backtrace
proc do |output, exception, _| output.puts "#{exception.class}: #{exception.}" output.puts "from #{exception.backtrace.first}" end
- DEFAULT_EXCEPTION_WHITELIST =
Don't catch these exceptions
[SystemExit, SignalException]
- DEFAULT_PROMPT =
The default prompt; includes the target and nesting level
[ proc { |target_self, nest_level, pry| "[#{pry.input_array.size}] pry(#{Pry.view_clip(target_self)})#{":#{nest_level}" unless nest_level.zero?}> " }, proc { |target_self, nest_level, pry| "[#{pry.input_array.size}] pry(#{Pry.view_clip(target_self)})#{":#{nest_level}" unless nest_level.zero?}* " } ]
- SIMPLE_PROMPT =
A simple prompt - doesn't display target or nesting level
[proc { ">> " }, proc { " | " }]
- SHELL_PROMPT =
[ proc { |target_self, _, _| "pry #{Pry.view_clip(target_self)}:#{Dir.pwd} $ " }, proc { |target_self, _, _| "pry #{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 |_, level, pry| tree = pry.binding_stack.map { |b| Pry.view_clip(b.eval("self")) }.join " / " "[#{pry.input_array.size}] (pry) #{tree}: #{level}> " end, proc do |_, level, pry| tree = pry.binding_stack.map { |b| Pry.view_clip(b.eval("self")) }.join " / " "[#{pry.input_array.size}] (pry) #{tree}: #{level}* " end, ]
- DEFAULT_CONTROL_D_HANDLER =
Deal with the ^D key being pressed, different behaviour in different cases: 1) In an expression - behave like
!command (clear input buffer) 2) At top-level session - behave likeexit command (break out of repl loop) 3) In a nested session - behave likecd ..` (pop a binding) proc do |eval_string, _pry_| if !eval_string.empty? # clear input buffer eval_string.replace("") elsif _pry_.binding_stack.one? # ^D at top-level breaks out of loop _pry_.binding_stack.clear throw(:breakout) else # otherwise just pops a binding _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
- VERSION =
"0.9.9.6"- Commands =
Default commands used by Pry.
Pry::CommandSet.new do import DefaultCommands::Misc import DefaultCommands::Help import DefaultCommands::Gems import DefaultCommands::Context import DefaultCommands::NavigatingPry import DefaultCommands::Editing import DefaultCommands::InputAndOutput import DefaultCommands::Introspection import DefaultCommands::EasterEggs import DefaultCommands::Commands end
- RC_FILES =
The RC Files to load.
["~/.pryrc", "./.pryrc"]
- DEFAULT_CUSTOM_COMPLETIONS =
This proc will be instance_eval's against the active Pry instance
proc { commands.commands.keys }
- FILE_COMPLETIONS =
proc { commands.commands.keys + Dir.entries('.') }
Class Attribute Summary (collapse)
-
+ (Boolean) cli
Whether Pry was activated from the command line.
-
+ (OpenStruct) config
Return Pry's config object.
-
+ (Fixnum) current_line
The current input line.
-
+ (Proc) custom_completions
Get/Set the Proc that defines extra Readline completions (on top of the ones defined for IRB).
-
+ (String) eval_path
The FILE for the
eval(). -
+ (History) history
Return Pry's line history object.
-
+ (Array) line_buffer
The Array of evaluated expressions.
-
+ (Boolean) quiet
Whether Pry sessions are quiet by default.
Instance Attribute Summary (collapse)
-
- (Object) backtrace
Returns the value of attribute backtrace.
-
- (Object) binding_stack
Returns the value of attribute binding_stack.
-
- (Object) commands
Returns the value of attribute commands.
-
- (Object) custom_completions
Returns the value of attribute custom_completions.
-
- (Object) exception_handler
Returns the value of attribute exception_handler.
-
- (Object) extra_sticky_locals
Returns the value of attribute extra_sticky_locals.
-
- (Object) hooks
Special treatment for hooks as we want to alert people of the changed API.
-
- (Object) input
Returns the value of attribute input.
-
- (Object) input_array
readonly
Returns the value of attribute input_array.
-
- (Object) input_stack
Returns the value of attribute input_stack.
-
- (Object) last_dir
Returns the value of attribute last_dir.
-
- (Object) last_exception
Returns the value of attribute last_exception.
-
- (Object) last_file
Returns the value of attribute last_file.
-
- (Object) last_result
Returns the value of attribute last_result.
-
- (Object) output
Returns the value of attribute output.
-
- (Object) output_array
readonly
Returns the value of attribute output_array.
-
- (Object) print
Returns the value of attribute print.
-
- (Object) quiet
(also: #quiet?)
Returns the value of attribute quiet.
Class Method Summary (collapse)
-
+ (Binding) binding_for(target)
Return a
Bindingobject fortargetor returntargetif it is already aBinding. -
+ (Object) Code(obj)
Convert the given object into an instance of
Pry::Code, if it isn't already one. - + (Object) default_editor_for_platform
-
+ (Object) delegate_accessors(delagatee, *names)
convenience method.
-
+ (Object) fix_coderay_colors
To avoid mass-confusion, we change the default colour of "white" to "blue" enabling global legibility.
-
+ (Object) init
Basic initialization.
-
+ (Boolean) initial_session?
Whether this is the first time a Pry session has been started since loading the Pry class.
-
+ (Object) initial_session_setup
Do basic setup for initial session.
-
+ (Object) load_history
Load Readline history if required.
-
+ (Object) load_rc
Load the rc files given in the
Pry::RC_FILESarray. -
+ (Object) load_requires
Load any Ruby files specified with the -r flag on the command line.
-
+ (Object) load_traps
Trap interrupts on jruby, and make them behave like MRI so we can catch them.
-
+ (Object) Method(obj)
If the given object is a
Pry::Method, return it unaltered. -
+ (Object) reset_defaults
Set all the configurable options back to their default values.
-
+ (Object) run_command(command_string, options = {})
Run a Pry command from outside a session.
-
+ (Object) save_history
Save new lines of Readline history if required.
- + (Object) set_config_defaults
-
+ (Object) start(target = TOPLEVEL_BINDING, options = {})
Start a Pry REPL.
-
+ (String) view_clip(obj, max_length = 60)
An inspector that clips the output to
max_lengthchars. -
+ (Object) WrappedModule(obj)
If the given object is a
Pry::WrappedModule, return it unaltered.
Instance Method Summary (collapse)
-
- (Object) add_sticky_local(name) { ... }
Add a sticky local to this Pry instance.
-
- (Binding) current_context
The currently active
Binding. -
- (Object, Exception) exec_hook(name, *args, &block)
Execute the specified hook.
-
- (Object) handle_read_errors
private
Manage switching of input objects on encountering EOFErrors.
-
- (Pry) initialize(options = {})
constructor
Create a new
Pryobject. -
- (Object) inject_local(name, value, b)
Injects a local variable into the provided binding.
-
- (Object) inject_sticky_locals(target)
Inject all the sticky locals into the
targetbinding. -
- (Boolean) last_result_is_exception?
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).
-
- (Integer) memory_size
The maximum amount of objects remembered by the inp and out arrays.
- - (Object) memory_size=(size)
-
- (Array<Proc>) pop_prompt
Pops the current prompt off of the prompt stack.
-
- (Boolean) process_command(val, eval_string, target)
If the given line is a valid command, process it in the context of the current
eval_stringand context. -
- (Array<Proc>) prompt
The current prompt.
- - (Object) prompt=(new_prompt)
-
- (Object) prompt_stack
private
the array that the prompt stack is stored in.
-
- (Array<Proc>) push_prompt(new_prompt)
Pushes the current prompt onto a stack that it can be restored from later.
-
- (String) r(target = TOPLEVEL_BINDING, eval_string = "")
Perform a read.
- - (Object) raise_up(*args)
- - (Object) raise_up!(*args)
-
- (Object) raise_up_common(force, *args)
Raise an exception out of Pry.
-
- (Object) re(target = TOPLEVEL_BINDING)
Perform a read-eval If no parameter is given, default to top-level (main).
-
- (String) readline(current_prompt = "> ", completion_proc = nil)
Returns the next line of input to be used by the pry instance.
-
- (Object) refresh(options = {})
Refresh the Pry instance settings from the Pry class.
-
- (Object) rep(target = TOPLEVEL_BINDING)
Perform a read-eval-print.
-
- (Object) repl(target = TOPLEVEL_BINDING)
Start a read-eval-print-loop.
-
- (Object) repl_epilogue(target)
Clean-up after the repl session.
-
- (Object) repl_prologue(target)
Initialize the repl session.
-
- (String) retrieve_line(eval_string, target)
Read and process a line of input -- check for ^D, determine which prompt to use, rewrite the indentation if
Pry.config.auto_indentis enabled, and, if the line is a command, process it and alter the eval_string accordingly. -
- (Pry::Command::VOID_VALUE) run_command(val, eval_string = "", target = binding_stack.last)
Run the specified command.
-
- (String) select_prompt(eval_string, target)
Returns the appropriate prompt to use.
-
- (Object) set_last_result(result, target, code = "")
Set the last result of an eval.
- - (Boolean) should_force_encoding?(eval_string, val) private
-
- (Boolean) should_print?
Whether the print proc should be invoked.
-
- (Object) show_result(result)
Output the result or pass to an exception handler (if result is an exception).
-
- (Hash) sticky_locals
The currently defined sticky locals.
-
- (Object) update_input_history(code)
Update Pry's internal state after evalling code.
Constructor Details
- (Pry) initialize(options = {})
Create a new Pry object.
57 58 59 60 61 62 |
# File 'lib/pry/pry_instance.rb', line 57 def initialize(={}) refresh() @binding_stack = [] @indent = Pry::Indent.new end |
Class Attribute Details
+ (Boolean) cli
Whether Pry was activated from the command line.
44 45 46 |
# File 'lib/pry/pry_class.rb', line 44 def cli @cli end |
+ (OpenStruct) config
Return Pry's config object.
38 39 40 |
# File 'lib/pry/pry_class.rb', line 38 def config @config end |
+ (Fixnum) current_line
The current input line.
28 29 30 |
# File 'lib/pry/pry_class.rb', line 28 def current_line @current_line end |
+ (Proc) custom_completions
Get/Set the Proc that defines extra Readline completions (on top of the ones defined for IRB).
25 26 27 |
# File 'lib/pry/pry_class.rb', line 25 def custom_completions @custom_completions end |
+ (String) eval_path
The FILE for the eval(). Should be "(pry)"
by default.
35 36 37 |
# File 'lib/pry/pry_class.rb', line 35 def eval_path @eval_path end |
+ (History) history
Return Pry's line history object.
41 42 43 |
# File 'lib/pry/pry_class.rb', line 41 def history @history end |
+ (Array) line_buffer
The Array of evaluated expressions.
31 32 33 |
# File 'lib/pry/pry_class.rb', line 31 def line_buffer @line_buffer end |
+ (Boolean) quiet
Whether Pry sessions are quiet by default.
47 48 49 |
# File 'lib/pry/pry_class.rb', line 47 def quiet @quiet end |
Instance Attribute Details
- (Object) backtrace
Returns the value of attribute backtrace
27 28 29 |
# File 'lib/pry/pry_instance.rb', line 27 def backtrace @backtrace end |
- (Object) binding_stack
Returns the value of attribute binding_stack
16 17 18 |
# File 'lib/pry/pry_instance.rb', line 16 def binding_stack @binding_stack end |
- (Object) commands
Returns the value of attribute commands
7 8 9 |
# File 'lib/pry/pry_instance.rb', line 7 def commands @commands end |
- (Object) custom_completions
Returns the value of attribute custom_completions
14 15 16 |
# File 'lib/pry/pry_instance.rb', line 14 def custom_completions @custom_completions end |
- (Object) exception_handler
Returns the value of attribute exception_handler
9 10 11 |
# File 'lib/pry/pry_instance.rb', line 9 def exception_handler @exception_handler end |
- (Object) extra_sticky_locals
Returns the value of attribute extra_sticky_locals
29 30 31 |
# File 'lib/pry/pry_instance.rb', line 29 def extra_sticky_locals @extra_sticky_locals end |
- (Object) hooks
Special treatment for hooks as we want to alert people of the changed API
33 34 35 |
# File 'lib/pry/pry_instance.rb', line 33 def hooks @hooks end |
- (Object) input
Returns the value of attribute input
5 6 7 |
# File 'lib/pry/pry_instance.rb', line 5 def input @input end |
- (Object) input_array (readonly)
Returns the value of attribute input_array
24 25 26 |
# File 'lib/pry/pry_instance.rb', line 24 def input_array @input_array end |
- (Object) input_stack
Returns the value of attribute input_stack
10 11 12 |
# File 'lib/pry/pry_instance.rb', line 10 def input_stack @input_stack end |
- (Object) last_dir
Returns the value of attribute last_dir
20 21 22 |
# File 'lib/pry/pry_instance.rb', line 20 def last_dir @last_dir end |
- (Object) last_exception
Returns the value of attribute last_exception
22 23 24 |
# File 'lib/pry/pry_instance.rb', line 22 def last_exception @last_exception end |
- (Object) last_file
Returns the value of attribute last_file
19 20 21 |
# File 'lib/pry/pry_instance.rb', line 19 def last_file @last_file end |
- (Object) last_result
Returns the value of attribute last_result
18 19 20 |
# File 'lib/pry/pry_instance.rb', line 18 def last_result @last_result end |
- (Object) output
Returns the value of attribute output
6 7 8 |
# File 'lib/pry/pry_instance.rb', line 6 def output @output end |
- (Object) output_array (readonly)
Returns the value of attribute output_array
25 26 27 |
# File 'lib/pry/pry_instance.rb', line 25 def output_array @output_array end |
- (Object) print
Returns the value of attribute print
8 9 10 |
# File 'lib/pry/pry_instance.rb', line 8 def print @print end |
- (Object) quiet Also known as: quiet?
Returns the value of attribute quiet
11 12 13 |
# File 'lib/pry/pry_instance.rb', line 11 def quiet @quiet end |
Class Method Details
+ (Binding) binding_for(target)
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
349 350 351 352 353 354 355 356 357 358 359 |
# File 'lib/pry/pry_class.rb', line 349 def self.binding_for(target) if Binding === target target else if TOPLEVEL_BINDING.eval('self') == target TOPLEVEL_BINDING else target.__binding__ end end end |
+ (Object) Code(obj)
Convert the given object into an instance of Pry::Code, if it isn't
already one.
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/pry/code.rb', line 8 def Code(obj) case obj when Code obj when ::Method, UnboundMethod, Proc, Pry::Method Code.from_method(obj) else Code.new(obj) end end |
+ (Object) default_editor_for_platform
205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/pry/pry_class.rb', line 205 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 |
+ (Object) delegate_accessors(delagatee, *names)
convenience method
15 16 17 18 |
# File 'lib/pry/pry_class.rb', line 15 def self.delegate_accessors(delagatee, *names) def_delegators delagatee, *names def_delegators delagatee, *names.map { |v| "#{v}=" } end |
+ (Object) fix_coderay_colors
To avoid mass-confusion, we change the default colour of "white" to "blue" enabling global legibility
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 |
# File 'lib/pry/pry_class.rb', line 318 def self.fix_coderay_colors to_fix = if (CodeRay::Encoders::Terminal::TOKEN_COLORS rescue nil) # CodeRay 1.0.0 CodeRay::Encoders::Terminal::TOKEN_COLORS else # CodeRay 0.9 begin require 'coderay/encoders/term' CodeRay::Encoders::Term::TOKEN_COLORS rescue end end to_fix[:comment] = "0;34" if to_fix end |
+ (Object) init
Basic initialization.
335 336 337 338 339 340 341 342 |
# File 'lib/pry/pry_class.rb', line 335 def self.init @plugin_manager ||= PluginManager.new self.config ||= Config.new self.history ||= History.new reset_defaults locate_plugins end |
+ (Boolean) initial_session?
Whether this is the first time a Pry session has been started since loading the Pry class.
172 173 174 |
# File 'lib/pry/pry_class.rb', line 172 def self.initial_session? @initial_session end |
+ (Object) initial_session_setup
Do basic setup for initial session. Including: loading .pryrc, loading plugins, loading requires, and loading history.
85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/pry/pry_class.rb', line 85 def self.initial_session_setup return if !initial_session? # 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 if Pry.config.should_load_rc 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 @initial_session = false end |
+ (Object) load_history
Load Readline history if required.
161 162 163 |
# File 'lib/pry/pry_class.rb', line 161 def self.load_history Pry.history.load end |
+ (Object) load_rc
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.
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/pry/pry_class.rb', line 58 def self.load_rc files = RC_FILES.collect { |file_name| File.(file_name) }.uniq files.each do |file_name| begin load(file_name) if File.exists?(file_name) rescue RescuableException => e puts "Error loading #{file_name}: #{e}" end end end |
+ (Object) load_requires
Load any Ruby files specified with the -r flag on the command line.
70 71 72 73 74 |
# File 'lib/pry/pry_class.rb', line 70 def self.load_requires Pry.config.requires.each do |file| require file end end |
+ (Object) load_traps
Trap interrupts on jruby, and make them behave like MRI so we can catch them.
78 79 80 |
# File 'lib/pry/pry_class.rb', line 78 def self.load_traps trap('INT'){ raise Interrupt } end |
+ (Object) Method(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.
8 9 10 11 12 13 14 |
# File 'lib/pry/method.rb', line 8 def Method(obj) if obj.is_a? Pry::Method obj else Pry::Method.new(obj) end end |
+ (Object) reset_defaults
Set all the configurable options back to their default values
302 303 304 305 306 307 308 309 310 311 312 313 314 |
# File 'lib/pry/pry_class.rb', line 302 def self.reset_defaults set_config_defaults @initial_session = true self.custom_completions = DEFAULT_CUSTOM_COMPLETIONS self.cli = false self.current_line = 1 self.line_buffer = [""] self.eval_path = "(pry)" fix_coderay_colors end |
+ (Object) run_command(command_string, options = {})
Run a Pry command from outside a session. The commands available are
those referenced by Pry.commands (the default command set).
192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/pry/pry_class.rb', line 192 def self.run_command(command_string, ={}) = { :context => TOPLEVEL_BINDING, :show_output => true, :output => Pry.output, :commands => Pry.commands }.merge!() output = [:show_output] ? [:output] : StringIO.new Pry.new(:output => output, :input => StringIO.new(command_string), :commands => [:commands], :prompt => proc {""}, :hooks => Pry::Hooks.new).rep([:context]) end |
+ (Object) save_history
Save new lines of Readline history if required.
166 167 168 |
# File 'lib/pry/pry_class.rb', line 166 def self.save_history Pry.history.save end |
+ (Object) set_config_defaults
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 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 |
# File 'lib/pry/pry_class.rb', line 218 def self.set_config_defaults config.input = Readline config.output = $stdout config.commands = Pry::Commands config.prompt = DEFAULT_PROMPT config.print = DEFAULT_PRINT config.exception_handler = DEFAULT_EXCEPTION_HANDLER config.exception_whitelist = DEFAULT_EXCEPTION_WHITELIST config.default_window_size = 5 config.hooks = DEFAULT_HOOKS config.input_stack = [] config.color = Helpers::BaseHelpers.use_ansi_codes? config.pager = true config.system = DEFAULT_SYSTEM config.editor = default_editor_for_platform config.should_load_rc = true config.should_trap_interrupts = Helpers::BaseHelpers.jruby? config.disable_auto_reload = false config.command_prefix = "" config.auto_indent = true config.correct_indent = true config.collision_warning = false config.gist ||= OpenStruct.new config.gist.inspecter = proc(&:pretty_inspect) config.should_load_plugins = true config.requires ||= [] config.should_load_requires = true config.history ||= OpenStruct.new config.history.should_save = true config.history.should_load = true config.history.file = File.("~/.pry_history") rescue nil if config.history.file.nil? config.should_load_rc = false config.history.should_save = false config.history.should_load = false end config.control_d_handler = DEFAULT_CONTROL_D_HANDLER config.memory_size = 100 config.extra_sticky_locals = {} config.ls ||= OpenStruct.new({ :heading_color => :default, :public_method_color => :default, :private_method_color => :green, :protected_method_color => :yellow, :method_missing_color => :bright_red, :local_var_color => :default, :pry_var_color => :red, # e.g. _, _pry_, _file_ :instance_var_color => :blue, # e.g. @foo :class_var_color => :bright_blue, # e.g. @@foo :global_var_color => :default, # e.g. $CODERAY_DEBUG, $eventmachine_library :builtin_global_color => :cyan, # e.g. $stdin, $-w, $PID :pseudo_global_color => :cyan, # e.g. $~, $1..$9, $LAST_MATCH_INFO :constant_color => :default, # e.g. VERSION, ARGF :class_constant_color => :blue, # e.g. Object, Kernel :exception_constant_color => :magenta, # e.g. Exception, RuntimeError :unloaded_constant_color => :yellow, # Any constant that is still in .autoload? state # What should separate items listed by ls? (TODO: we should allow a columnar layout) :separator => " ", # Any methods defined on these classes, or modules included into these classes, will not # be shown by ls unless the -v flag is used. # A user of Rails may wih to add ActiveRecord::Base to the list. # add the following to your .pryrc: # Pry.config.ls.ceiling << ActiveRecord::Base if defined? ActiveRecordBase :ceiling => [Object, Module, Class] }) end |
+ (Object) start(target = TOPLEVEL_BINDING, options = {})
Start a Pry REPL.
This method also loads the files specified in Pry::RC_FILES the
first time it is invoked.
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/pry/pry_class.rb', line 108 def self.start(target=TOPLEVEL_BINDING, ={}) target = Pry.binding_for(target) initial_session_setup # create the Pry instance to manage the session pry_instance = new() # save backtrace pry_instance.backtrace = caller # if Pry was started via binding.pry, elide that from the backtrace. pry_instance.backtrace.shift if pry_instance.backtrace.first =~ /pry.*core_extensions.*pry/ # yield the binding_stack to the hook for modification pry_instance.exec_hook( :when_started, target, , pry_instance ) if !pry_instance.binding_stack.empty? head = pry_instance.binding_stack.pop else head = target end # Enter the matrix pry_instance.repl(head) end |
+ (String) view_clip(obj, max_length = 60)
An inspector that clips the output to max_length chars.
In case of > max_length chars the `#
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/pry/pry_class.rb', line 144 def self.view_clip(obj, max_length = 60) if obj.kind_of?(Module) && obj.name.to_s != "" && obj.name.to_s.length <= max_length obj.name.to_s elsif TOPLEVEL_BINDING.eval('self') == obj # special case for 'main' object :) obj.to_s elsif [String, Numeric, Symbol, nil, true, false].any? { |v| v === obj } && obj.inspect.length <= max_length obj.inspect else "#<#{obj.class}>"#:%x>"# % (obj.object_id << 1) end rescue RescuableException "unknown" end |
+ (Object) WrappedModule(obj)
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
- (Object) add_sticky_local(name) { ... }
Add a sticky local to this Pry instance. A sticky local is a local that persists between all bindings in a session.
149 150 151 |
# File 'lib/pry/pry_instance.rb', line 149 def add_sticky_local(name, &block) sticky_locals[name] = block end |
- (Binding) current_context
The currently active Binding.
89 90 91 |
# File 'lib/pry/pry_instance.rb', line 89 def current_context binding_stack.last end |
- (Object, Exception) exec_hook(name, *args, &block)
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.
445 446 447 448 449 450 451 452 453 454 |
# File 'lib/pry/pry_instance.rb', line 445 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 |
- (Object) handle_read_errors (private)
Manage switching of input objects on encountering EOFErrors
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 |
# File 'lib/pry/pry_instance.rb', line 511 def handle_read_errors should_retry = true begin yield rescue EOFError if input_stack.empty? self.input = Pry.config.input if !should_retry output.puts "Error: Pry ran out of things to read from! Attempting to break out of REPL." throw(:breakout) end should_retry = false else self.input = input_stack.pop end retry # Interrupts are handled in r() because they need to tweak eval_string # TODO: Refactor this baby. rescue Interrupt raise # If we get a random error when trying to read a line we don't want to automatically # retry, as the user will see a lot of error messages scroll past and be unable to do # anything about it. rescue RescuableException => e puts "Error: #{e.}" puts "FATAL: Pry failed to get user input using `#{input}`." puts "To fix this you may be able to pass input and output file descriptors to pry directly. e.g." puts " Pry.config.input = STDIN" puts " Pry.config.output = STDOUT" puts " binding.pry" throw(:breakout) end end |
- (Object) inject_local(name, value, b)
Injects a local variable into the provided binding.
118 119 120 121 122 123 |
# File 'lib/pry/pry_instance.rb', line 118 def inject_local(name, value, b) Thread.current[:__pry_local__] = value.is_a?(Proc) ? value.call : value b.eval("#{name} = Thread.current[:__pry_local__]") ensure Thread.current[:__pry_local__] = nil end |
- (Object) inject_sticky_locals(target)
Inject all the sticky locals into the target binding.
138 139 140 141 142 |
# File 'lib/pry/pry_instance.rb', line 138 def inject_sticky_locals(target) sticky_locals.each_pair do |name, value| inject_local(name, value, target) end end |
- (Boolean) last_result_is_exception?
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)
506 507 508 |
# File 'lib/pry/pry_instance.rb', line 506 def last_result_is_exception? @last_result_is_exception end |
- (Integer) memory_size
The maximum amount of objects remembered by the inp and out arrays. Defaults to 100.
127 128 129 |
# File 'lib/pry/pry_instance.rb', line 127 def memory_size @output_array.max_size end |
- (Object) memory_size=(size)
131 132 133 134 |
# File 'lib/pry/pry_instance.rb', line 131 def memory_size=(size) @input_array = Pry::HistoryArray.new(size) @output_array = Pry::HistoryArray.new(size) end |
- (Array<Proc>) pop_prompt
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.
632 633 634 |
# File 'lib/pry/pry_instance.rb', line 632 def pop_prompt prompt_stack.size > 1 ? prompt_stack.pop : prompt end |
- (Boolean) process_command(val, eval_string, target)
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.
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 |
# File 'lib/pry/pry_instance.rb', line 393 def process_command(val, eval_string, target) result = commands.process_line(val, { :target => target, :output => output, :eval_string => eval_string, :pry_instance => self }) # set a temporary (just so we can inject the value we want into eval_string) Thread.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 "Thread.current[:__pry_cmd_result__].retval\n" end true else false end end |
- (Array<Proc>) prompt
The current prompt. This is the prompt at the top of the prompt stack.
101 102 103 |
# File 'lib/pry/pry_instance.rb', line 101 def prompt prompt_stack.last end |
- (Object) prompt=(new_prompt)
105 106 107 108 109 110 111 |
# File 'lib/pry/pry_instance.rb', line 105 def prompt=(new_prompt) if prompt_stack.empty? push_prompt new_prompt else prompt_stack[-1] = new_prompt end end |
- (Object) prompt_stack (private)
the array that the prompt stack is stored in
604 605 606 |
# File 'lib/pry/pry_instance.rb', line 604 def prompt_stack @prompt_stack ||= Array.new end |
- (Array<Proc>) push_prompt(new_prompt)
Pushes the current prompt onto a stack that it can be restored from later. Use this if you wish to temporarily change the prompt.
616 617 618 |
# File 'lib/pry/pry_instance.rb', line 616 def push_prompt(new_prompt) prompt_stack.push new_prompt end |
- (String) r(target = TOPLEVEL_BINDING, eval_string = "")
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.
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 |
# File 'lib/pry/pry_instance.rb', line 268 def r(target=TOPLEVEL_BINDING, eval_string="") target = Pry.binding_for(target) @suppress_output = false loop do begin # eval_string will probably be mutated by this method retrieve_line(eval_string, target) rescue CommandError, Slop::InvalidOptionError => e output.puts "Error: #{e.}" end begin break if Pry::Code.complete_expression?(eval_string) rescue SyntaxError => e output.puts "SyntaxError: #{e..sub(/.*syntax error, */m, '')}" eval_string = "" end end @suppress_output = true if eval_string =~ /;\Z/ || eval_string.empty? exec_hook :after_read, eval_string, self eval_string end |
- (Object) raise_up(*args)
675 |
# File 'lib/pry/pry_instance.rb', line 675 def raise_up(*args); raise_up_common(false, *args); end |
- (Object) raise_up!(*args)
676 |
# File 'lib/pry/pry_instance.rb', line 676 def raise_up!(*args); raise_up_common(true, *args); end |
- (Object) raise_up_common(force, *args)
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")
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 |
# File 'lib/pry/pry_instance.rb', line 648 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 |
- (Object) re(target = TOPLEVEL_BINDING)
Perform a read-eval If no parameter is given, default to top-level (main).
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/pry/pry_instance.rb', line 237 def re(target=TOPLEVEL_BINDING) target = Pry.binding_for(target) # It's not actually redundant to inject them continually as we may have # moved into the scope of a new Binding (e.g the user typed `cd`) inject_sticky_locals(target) code = r(target) result = target.eval(code, Pry.eval_path, Pry.current_line) set_last_result(result, target, code) result rescue RescuableException => e self.last_exception = e e ensure update_input_history(code) exec_hook :after_eval, result, self end |
- (String) readline(current_prompt = "> ", completion_proc = nil)
Returns the next line of input to be used by the pry instance. This method should not need to be invoked directly.
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 |
# File 'lib/pry/pry_instance.rb', line 552 def readline(current_prompt="> ", completion_proc=nil) handle_read_errors do if defined? Coolline and input.is_a? Coolline input.completion_proc = proc do |cool| completion_proc.call cool.completed_word end elsif input.respond_to? :completion_proc= input.completion_proc = completion_proc end if input == Readline input.readline(current_prompt, false) # false since we'll add it manually elsif defined? Coolline and input.is_a? Coolline input.readline(current_prompt) else if input.method(:readline).arity == 1 input.readline(current_prompt) else input.readline end end end end |
- (Object) refresh(options = {})
Refresh the Pry instance settings from the Pry class. Allows options to be specified to override settings from Pry class.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/pry/pry_instance.rb', line 68 def refresh(={}) defaults = {} attributes = [ :input, :output, :commands, :print, :quiet, :exception_handler, :hooks, :custom_completions, :prompt, :memory_size, :input_stack, :extra_sticky_locals ] attributes.each do |attribute| defaults[attribute] = Pry.send attribute end defaults.merge!().each do |key, value| send("#{key}=", value) if respond_to?("#{key}=") end true end |
- (Object) rep(target = TOPLEVEL_BINDING)
Perform a read-eval-print. If no parameter is given, default to top-level (main).
222 223 224 225 226 227 |
# File 'lib/pry/pry_instance.rb', line 222 def rep(target=TOPLEVEL_BINDING) target = Pry.binding_for(target) result = re(target) show_result(result) if should_print? end |
- (Object) repl(target = TOPLEVEL_BINDING)
Start a read-eval-print-loop. If no parameter is given, default to top-level (main).
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/pry/pry_instance.rb', line 195 def repl(target=TOPLEVEL_BINDING) target = Pry.binding_for(target) repl_prologue(target) break_data = nil exception = catch(:raise_up) do break_data = catch(:breakout) do loop do rep(binding_stack.last) end end exception = false end raise exception if exception break_data ensure repl_epilogue(target) end |
- (Object) repl_epilogue(target)
Clean-up after the repl session.
180 181 182 183 184 185 |
# File 'lib/pry/pry_instance.rb', line 180 def repl_epilogue(target) exec_hook :after_session, output, target, self binding_stack.pop Pry.save_history if Pry.config.history.should_save end |
- (Object) repl_prologue(target)
Initialize the repl session.
168 169 170 171 172 173 174 175 176 |
# File 'lib/pry/pry_instance.rb', line 168 def repl_prologue(target) exec_hook :before_session, output, target, self set_last_result(nil, target) @input_array << nil # add empty input so _in_ and _out_ match binding_stack.push target end |
- (String) retrieve_line(eval_string, target)
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.
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 |
# File 'lib/pry/pry_instance.rb', line 329 def retrieve_line(eval_string, target) @indent.reset if eval_string.empty? current_prompt = select_prompt(eval_string, target) completion_proc = Pry::InputCompleter.build_completion_proc(target, instance_eval(&custom_completions)) indentation = Pry.config.auto_indent ? @indent.current_prefix : '' begin val = readline("#{current_prompt}#{indentation}", completion_proc) # Handle <Ctrl+C> like Bash, empty the current input buffer but do not quit. # This is only for ruby-1.9; other versions of ruby do not let you send Interrupt # from within Readline. rescue Interrupt output.puts "" eval_string.replace("") return end # invoke handler if we receive EOF character (^D) if !val output.puts "" Pry.config.control_d_handler.call(eval_string, self) return end # Change the eval_string into the input encoding (Issue 284) # TODO: This wouldn't be necessary if the eval_string was constructed from # input strings only. if should_force_encoding?(eval_string, val) eval_string.force_encoding(val.encoding) end if Pry.config.auto_indent && !input.is_a?(StringIO) original_val = "#{indentation}#{val}" indented_val = @indent.indent(val) if output.tty? && Pry::Helpers::BaseHelpers.use_ansi_codes? && Pry.config.correct_indent output.print @indent.correct_indentation(current_prompt, indented_val, original_val.length - indented_val.length) output.flush end else indented_val = val end begin if !process_command(val, eval_string, target) eval_string << "#{indented_val.rstrip}\n" unless val.empty? end ensure Pry.history << indented_val unless input.is_a?(StringIO) end end |
- (Pry::Command::VOID_VALUE) run_command(val, eval_string = "", target = binding_stack.last)
Run the specified command.
427 428 429 430 431 432 433 434 435 |
# File 'lib/pry/pry_instance.rb', line 427 def run_command(val, eval_string = "", target = binding_stack.last) commands.process_line(val, :eval_string => eval_string, :target => target, :pry_instance => self, :output => output ) Pry::Command::VOID_VALUE end |
- (String) select_prompt(eval_string, target)
Returns the appropriate prompt to use. This method should not need to be invoked directly.
590 591 592 593 594 595 596 597 598 599 600 601 |
# File 'lib/pry/pry_instance.rb', line 590 def select_prompt(eval_string, target) target_self = target.eval('self') # If input buffer is empty then use normal prompt if eval_string.empty? Array(prompt).first.call(target_self, binding_stack.size - 1, self) # Otherwise use the wait prompt (indicating multi-line expression) else Array(prompt).last.call(target_self, binding_stack.size - 1, self) end end |
- (Object) set_last_result(result, target, code = "")
Set the last result of an eval. This method should not need to be invoked directly.
461 462 463 464 465 466 |
# File 'lib/pry/pry_instance.rb', line 461 def set_last_result(result, target, code="") @last_result_is_exception = false @output_array << result self.last_result = result unless code =~ /\A\s*\z/ end |
- (Boolean) should_force_encoding?(eval_string, val) (private)
316 317 318 |
# File 'lib/pry/pry_instance.rb', line 316 def should_force_encoding?(eval_string, val) eval_string.empty? && val.respond_to?(:encoding) && val.encoding != eval_string.encoding end |
- (Boolean) should_print?
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.
581 582 583 |
# File 'lib/pry/pry_instance.rb', line 581 def should_print? !@suppress_output || last_result_is_exception? end |
- (Object) show_result(result)
Output the result or pass to an exception handler (if result is an exception).
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 |
# File 'lib/pry/pry_instance.rb', line 295 def show_result(result) if last_result_is_exception? exception_handler.call output, result, self else print.call output, result 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 end |
- (Hash) sticky_locals
The currently defined sticky locals.
154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/pry/pry_instance.rb', line 154 def sticky_locals @sticky_locals ||= { :_in_ => proc { @input_array }, :_out_ => proc { @output_array }, :_pry_ => self, :_ex_ => proc { last_exception }, :_file_ => proc { last_file }, :_dir_ => proc { last_dir }, :_ => proc { last_result } }.merge(extra_sticky_locals) end |
- (Object) update_input_history(code)
Update Pry's internal state after evalling code. This method should not need to be invoked directly.
494 495 496 497 498 499 500 501 |
# File 'lib/pry/pry_instance.rb', line 494 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.each_line.count end end |