pry 0.3.0 → 0.4.0pre1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG +10 -0
- data/README.markdown +442 -29
- data/lib/pry.rb +10 -247
- data/lib/pry/command_base.rb +114 -0
- data/lib/pry/commands.rb +115 -0
- data/lib/pry/completion.rb +202 -0
- data/lib/pry/core_extensions.rb +47 -0
- data/lib/pry/hooks.rb +8 -0
- data/lib/pry/print.rb +15 -0
- data/lib/pry/prompts.rb +24 -0
- data/lib/pry/pry_class.rb +109 -0
- data/lib/pry/pry_instance.rb +309 -0
- data/lib/pry/version.rb +1 -1
- data/test/test.rb +528 -0
- data/test/test_helper.rb +38 -0
- metadata +21 -15
- data/lib/pry/input.rb +0 -22
- data/lib/pry/output.rb +0 -100
data/CHANGELOG
CHANGED
@@ -1,3 +1,13 @@
|
|
1
|
+
21/1/2010 version 0.4.0
|
2
|
+
* added command API
|
3
|
+
* added many new commands, i.e ls_methods and friends
|
4
|
+
* modified other commands
|
5
|
+
* now accepts greater customization, can modify: input, output, hooks,
|
6
|
+
prompt, print object
|
7
|
+
* added tab completion (even completes commands)
|
8
|
+
* added extensive tests
|
9
|
+
* added examples
|
10
|
+
* many more changes
|
1
11
|
9/12/2010 version 0.1.3
|
2
12
|
* Got rid of rubygems dependency, refactored some code.
|
3
13
|
8/12/2010 version 0.1.2
|
data/README.markdown
CHANGED
@@ -6,29 +6,29 @@ Pry
|
|
6
6
|
_attach an irb-like session to any object at runtime_
|
7
7
|
|
8
8
|
Pry is a simple Ruby REPL (Read-Eval-Print-Loop) that specializes in the interactive
|
9
|
-
manipulation of objects during the running of a program.
|
9
|
+
manipulation of objects during the running of a program.
|
10
10
|
|
11
11
|
It is not based on the IRB codebase, and implements some unique REPL
|
12
|
-
commands such as `show_method` and `
|
12
|
+
commands such as `show_method` and `show_doc`
|
13
13
|
|
14
14
|
* Install the [gem](https://rubygems.org/gems/pry): `gem install pry`
|
15
15
|
* Read the [documentation](http://rdoc.info/github/banister/pry/master/file/README.markdown)
|
16
16
|
* See the [source code](http://github.com/banister/pry)
|
17
17
|
|
18
|
-
Example: Interacting with an object at runtime
|
18
|
+
Example: Interacting with an object at runtime
|
19
19
|
---------------------------------------
|
20
20
|
|
21
|
-
With the `
|
21
|
+
With the `Object#pry` method we can pry (open an irb-like session) on
|
22
22
|
an object. In the example below we open a Pry session for the `Test` class and execute a method and add
|
23
23
|
an instance variable. The current thread is halted for the duration of the session.
|
24
24
|
|
25
25
|
require 'pry'
|
26
|
-
|
26
|
+
|
27
27
|
class Test
|
28
28
|
def self.hello() "hello world" end
|
29
29
|
end
|
30
30
|
|
31
|
-
|
31
|
+
Test.pry
|
32
32
|
|
33
33
|
# Pry session begins on stdin
|
34
34
|
Beginning Pry session for Test
|
@@ -48,23 +48,23 @@ effect:
|
|
48
48
|
|
49
49
|
Test.instance_variable_get(:@y) #=> 20
|
50
50
|
|
51
|
-
|
51
|
+
### Alternative Syntax
|
52
52
|
|
53
|
-
You can also use the `obj
|
53
|
+
You can also use the `Pry.start(obj)` or `pry(obj)` syntax to start a pry session on
|
54
54
|
`obj`. e.g
|
55
55
|
|
56
|
-
5
|
56
|
+
Pry.start(5)
|
57
57
|
Beginning Pry session for 5
|
58
58
|
pry(5)>
|
59
59
|
|
60
60
|
OR
|
61
61
|
|
62
|
-
pry
|
62
|
+
pry(6)
|
63
63
|
beginning Pry session for 6
|
64
64
|
pry(6)>
|
65
|
-
|
66
|
-
Example: Pry sessions can nest
|
67
|
-
|
65
|
+
|
66
|
+
Example: Pry sessions can nest
|
67
|
+
-----------------------------------------------
|
68
68
|
|
69
69
|
Here we will begin Pry at top-level, then pry on a class and then on
|
70
70
|
an instance variable inside that class:
|
@@ -76,11 +76,11 @@ an instance variable inside that class:
|
|
76
76
|
pry(main)* @x = 20
|
77
77
|
pry(main)* end
|
78
78
|
=> 20
|
79
|
-
pry(main)>
|
79
|
+
pry(main)> Hello.pry
|
80
80
|
Beginning Pry session for Hello
|
81
81
|
pry(Hello):1> instance_variables
|
82
82
|
=> [:@x]
|
83
|
-
pry(Hello):1>
|
83
|
+
pry(Hello):1> @x.pry
|
84
84
|
Beginning Pry session for 20
|
85
85
|
pry(20:2)> self + 10
|
86
86
|
=> 30
|
@@ -102,7 +102,7 @@ command. E.g
|
|
102
102
|
2. 100
|
103
103
|
3. "friend"
|
104
104
|
=> nil
|
105
|
-
|
105
|
+
|
106
106
|
We can then jump back to any of the previous nesting levels by using
|
107
107
|
the `jump_to` command:
|
108
108
|
|
@@ -112,7 +112,7 @@ the `jump_to` command:
|
|
112
112
|
=> 100
|
113
113
|
pry(Hello):1>
|
114
114
|
|
115
|
-
If we just want to go back one level of nesting we can of course
|
115
|
+
If we just want to go back one level of nesting we can of course
|
116
116
|
use the `quit` or `exit` or `back` commands.
|
117
117
|
|
118
118
|
To break out of all levels of Pry nesting and return immediately to the
|
@@ -124,7 +124,7 @@ calling process use `exit_all`:
|
|
124
124
|
Ending Pry session for Hello
|
125
125
|
Ending Pry session for main
|
126
126
|
=> main
|
127
|
-
|
127
|
+
|
128
128
|
# program resumes here
|
129
129
|
|
130
130
|
Features and limitations
|
@@ -138,16 +138,19 @@ uses (such as implementing a quake-like console for games, for example). Here is
|
|
138
138
|
list of Pry's features along with some of its limitations given at the
|
139
139
|
end.
|
140
140
|
|
141
|
-
|
141
|
+
###Features:
|
142
142
|
|
143
143
|
* Pry can be invoked at any time and on any object in the running program.
|
144
144
|
* Pry sessions can nest arbitrarily deeply -- to go back one level of nesting type 'exit' or 'quit' or 'back'
|
145
145
|
* Use `_` to recover last result.
|
146
|
+
* Use `_pry_` to reference the Pry instance managing the current session.
|
147
|
+
* Pry supports tab completion.
|
146
148
|
* Pry has multi-line support built in.
|
147
|
-
* Pry has
|
149
|
+
* Pry has special commands not found in many other Ruby REPLs: `show_method`, `show_doc`
|
148
150
|
`jump_to`, `ls`, `cd`, `cat`
|
149
151
|
* Pry gives good control over nested sessions (important when exploring complicated runtime state)
|
150
152
|
* Pry is not based on the IRB codebase.
|
153
|
+
* Pry allows significant customizability.
|
151
154
|
* Pry uses [RubyParser](https://github.com/seattlerb/ruby_parser) to
|
152
155
|
validate expressions in 1.8, and [Ripper](http://rdoc.info/docs/ruby-core/1.9.2/Ripper) for 1.9.
|
153
156
|
* Pry implements all the methods in the REPL chain separately: `Pry#r`
|
@@ -155,15 +158,15 @@ for reading; `Pry#re` for eval; `Pry#rep` for printing; and `Pry#repl`
|
|
155
158
|
for the loop (`Pry.start` simply wraps `Pry.new.repl`). You can
|
156
159
|
invoke any of these methods directly depending on exactly what aspect of the functionality you need.
|
157
160
|
|
158
|
-
|
161
|
+
###Limitations:
|
159
162
|
|
160
163
|
* Pry does not pretend to be a replacement for `irb`,
|
161
164
|
and so does not have an executable. It is designed to be used by
|
162
165
|
other programs, not on its own. For a full-featured `irb` replacement
|
163
166
|
see [ripl](https://github.com/cldwalker/ripl)
|
164
|
-
* Pry's `show_method` and `
|
167
|
+
* Pry's `show_method` and `show_doc` commands do not work
|
165
168
|
in Ruby 1.8.
|
166
|
-
|
169
|
+
|
167
170
|
Commands
|
168
171
|
-----------
|
169
172
|
|
@@ -173,11 +176,26 @@ Commands
|
|
173
176
|
receives as a parameter. In the case of no parameter it operates on
|
174
177
|
top-level (main). It can receive any object or a `Binding`
|
175
178
|
object as parameter. `Pry.start()` is implemented as `Pry.new.repl()`
|
176
|
-
* `obj.pry` and `pry(obj)` may also be used as alternative syntax to
|
179
|
+
* `obj.pry` and `pry(obj)` may also be used as alternative syntax to
|
180
|
+
`Pry.start(obj)`.
|
181
|
+
|
182
|
+
However there are some differences. `obj.pry` opens
|
183
|
+
a Pry session on the receiver whereas `Pry.start` (with no parameter)
|
184
|
+
will start a Pry session on top-level. The other form of the `pry`
|
185
|
+
method: `pry(obj)` will also start a Pry session on its parameter.
|
186
|
+
|
187
|
+
The `pry` method invoked by itself, with no explict receiver and no
|
188
|
+
parameter will start a Pry session on the implied receiver. It is
|
189
|
+
perhaps more useful to invoke it in this form `pry(binding)` or
|
190
|
+
`binding.pry` so as to get access to locals in the current context.
|
191
|
+
|
192
|
+
Another difference is that `Pry.start()` accepts a second parameter
|
193
|
+
that is a hash of configuration options (discussed further, below).
|
194
|
+
|
177
195
|
* If, for some reason you do not want to 'loop' then use `Pry.new.rep()`; it
|
178
196
|
only performs the Read-Eval-Print section of the REPL - it ends the
|
179
197
|
session after just one line of input. It takes the same parameters as
|
180
|
-
`Pry#repl()`
|
198
|
+
`Pry#repl()`
|
181
199
|
* Likewise `Pry#re()` only performs the Read-Eval section of the REPL,
|
182
200
|
it returns the result of the evaluation or an Exception object in
|
183
201
|
case of error. It also takes the same parameters as `Pry#repl()`
|
@@ -202,8 +220,10 @@ If you want to access a method of the same name, prefix the invocation by whites
|
|
202
220
|
are nested sessions).
|
203
221
|
* `ls` returns a list of local variables and instance variables in the
|
204
222
|
current scope
|
205
|
-
* `
|
206
|
-
* `
|
223
|
+
* `ls_methods` List all methods defined on immediate class of receiver.
|
224
|
+
* `ls_imethods` List all instance methods defined on receiver.
|
225
|
+
* `cat <var>` Calls `inspect` on `<var>`
|
226
|
+
* `cd <var>` Starts a `Pry` session on the variable <var>. E.g `cd @x`
|
207
227
|
* `show_method <methname>` Displays the sourcecode for the method
|
208
228
|
<methname>. E.g `show_method hello`
|
209
229
|
* `show_imethod <methname>` Displays the sourcecode for the
|
@@ -213,8 +233,10 @@ If you want to access a method of the same name, prefix the invocation by whites
|
|
213
233
|
method `<methname>`
|
214
234
|
* `exit_program` or `quit_program` will end the currently running
|
215
235
|
program.
|
216
|
-
* `nesting`
|
217
|
-
* `
|
236
|
+
* `nesting` Shows Pry nesting information.
|
237
|
+
* `!pry` Starts a Pry session on the implied receiver; this can be
|
238
|
+
used in the middle of an expression in multi-line input.
|
239
|
+
* `jump_to <nest_level>` Unwinds the Pry stack (nesting level) until the appropriate nesting level is reached
|
218
240
|
-- as per the output of `nesting`
|
219
241
|
* `exit_all` breaks out of all Pry nesting levels and returns to the
|
220
242
|
calling process.
|
@@ -222,6 +244,396 @@ If you want to access a method of the same name, prefix the invocation by whites
|
|
222
244
|
current one with `obj` as the receiver of the new session. Very useful
|
223
245
|
when exploring large or complicated runtime state.
|
224
246
|
|
247
|
+
Example Programs
|
248
|
+
----------------
|
249
|
+
|
250
|
+
Pry comes bundled with a few example programs to illustrate some
|
251
|
+
features, see the `examples/` directory.
|
252
|
+
|
253
|
+
* `example_input.rb` - Demonstrates how to set the `input` object.
|
254
|
+
* `example_output.rb` - Demonstrates how to set the `output` object.
|
255
|
+
* `example_hooks.rb` - Demonstrates how to set the `hooks` hash.
|
256
|
+
* `example_print.rb` - Demonstrates how to set the `print` object.
|
257
|
+
* `example_prompt.rb` - Demonstrates how to set the `prompt`.
|
258
|
+
* `example_input2.rb` - An advanced `input` example.
|
259
|
+
* `example_commands.rb` - Implementing a mathematical command set.
|
260
|
+
* `example_commands_override.rb` - An advanced `commands` example.
|
261
|
+
* `example_image_edit.rb` - A simple image editor using a Pry REPL (requires `Gosu` and `TexPlay` gems).
|
262
|
+
|
263
|
+
Customizing Pry
|
264
|
+
---------------
|
265
|
+
|
266
|
+
Pry supports customization of the input, the output, the commands,
|
267
|
+
the hooks, the prompt, and 'print' (the "P" in REPL).
|
268
|
+
|
269
|
+
Global customization, which applies to all Pry sessions, is done
|
270
|
+
through invoking class accessors on the `Pry` class, the accessors
|
271
|
+
are:
|
272
|
+
|
273
|
+
* `Pry.input=`
|
274
|
+
* `Pry.output=`
|
275
|
+
* `Pry.commands=`
|
276
|
+
* `Pry.hooks=`
|
277
|
+
* `Pry.prompt=`
|
278
|
+
* `Pry.print=`
|
279
|
+
|
280
|
+
Local customization (applied to a single Pry session) is done by
|
281
|
+
passing config hash options to `Pry.start()` or to `Pry.new()`; also the
|
282
|
+
same accessors as described above for the `Pry` class exist for a
|
283
|
+
Pry instance so that customization can occur during runtime.
|
284
|
+
|
285
|
+
### Input
|
286
|
+
|
287
|
+
For input Pry accepts any object that implements the `readline` method. This
|
288
|
+
includes `IO` objects, `StringIO`, `Readline` and custom objects. Pry
|
289
|
+
initially defaults to using `Readline` for input.
|
290
|
+
|
291
|
+
#### Example: Setting global input
|
292
|
+
|
293
|
+
Setting Pry's global input causes all subsequent Pry instances to use
|
294
|
+
this input by default:
|
295
|
+
|
296
|
+
Pry.input = StringIO.new("@x = 10\nexit")
|
297
|
+
Object.pry
|
298
|
+
|
299
|
+
Object.instance_variable_get(:@x) #=> 10
|
300
|
+
|
301
|
+
The above will execute the code in the `StringIO`
|
302
|
+
non-interactively. It gets all the input it needs from the `StringIO`
|
303
|
+
and then exits the Pry session. Note it is important to end the
|
304
|
+
session with 'exit' if you are running non-interactively or the Pry
|
305
|
+
session will hang as it loops indefinitely awaiting new input.
|
306
|
+
|
307
|
+
#### Example: Setting input for a specific session
|
308
|
+
|
309
|
+
The settings for a specific session override the global settings
|
310
|
+
(discussed above). There are two ways to set input for a specific pry session: At the
|
311
|
+
point the session is started, or within the session itself (at runtime):
|
312
|
+
|
313
|
+
##### At session start
|
314
|
+
|
315
|
+
Pry.start(Object, :input => StringIO.new("@x = 10\nexit"))
|
316
|
+
Object.instance_variable_get(:@x) #=> 10
|
317
|
+
|
318
|
+
##### At runtime
|
319
|
+
|
320
|
+
If you want to set the input object within the session itself you use
|
321
|
+
the special `_pry_` local variable which represents the Pry instance
|
322
|
+
managing the current session; inside the session we type:
|
323
|
+
|
324
|
+
_pry_.input = StringIO.new("@x = 10\nexit")
|
325
|
+
|
326
|
+
Note we can also set the input object for the parent Pry session (if
|
327
|
+
the current session is nested) like so:
|
328
|
+
|
329
|
+
_pry_.parent.input = StringIO.new("@x = 10\nexit")
|
330
|
+
|
331
|
+
### Output
|
332
|
+
|
333
|
+
For output Pry accepts any object that implements the `puts` method. This
|
334
|
+
includes `IO` objects, `StringIO` and custom objects. Pry initially
|
335
|
+
defaults to using `$stdout` for output.
|
336
|
+
|
337
|
+
#### Example: Setting global output
|
338
|
+
|
339
|
+
Setting Pry's global output causes all subsequent Pry instances to use
|
340
|
+
this output by default:
|
341
|
+
|
342
|
+
Pry.output = StringIO.new
|
343
|
+
|
344
|
+
#### Example: Setting output for a specific session
|
345
|
+
|
346
|
+
As per Input, given above, we set the local output as follows:
|
347
|
+
|
348
|
+
##### At session start
|
349
|
+
|
350
|
+
Pry.start(Object, :output => StringIO.new("@x = 10\nexit"))
|
351
|
+
|
352
|
+
##### At runtime
|
353
|
+
|
354
|
+
_pry_.output = StringIO.new
|
355
|
+
|
356
|
+
### Commands
|
357
|
+
|
358
|
+
Pry commands are not methods; they are commands that are intercepted
|
359
|
+
and executed before a Ruby eval takes place. Pry comes with a default
|
360
|
+
command set (`Pry::Commands`), but these commands can be augmented or overriden by
|
361
|
+
user-specified ones.
|
362
|
+
|
363
|
+
The Pry command API is quite sophisticated supporting features such as:
|
364
|
+
command set inheritance, importing of specific commands from another
|
365
|
+
command set, deletion of commands, calling of commands within other
|
366
|
+
commands, and so on.
|
367
|
+
|
368
|
+
A valid Pry command object must inherit from
|
369
|
+
`Pry::CommandBase` (or one of its subclasses) and use the special command API:
|
370
|
+
|
371
|
+
#### Example: Defining a command object and setting it globally
|
372
|
+
|
373
|
+
class MyCommands < Pry::CommandBase
|
374
|
+
command "greet", "Greet the user." do |name|
|
375
|
+
output.puts "Hello #{name.capitalize}, how are you?"
|
376
|
+
end
|
377
|
+
end
|
378
|
+
|
379
|
+
Pry.commands = MyCommands
|
380
|
+
|
381
|
+
Then inside a pry session:
|
382
|
+
|
383
|
+
pry(main)> greet john
|
384
|
+
hello John, how are you?
|
385
|
+
=> nil
|
386
|
+
|
387
|
+
#### Example: Using a command object in a specific session
|
388
|
+
|
389
|
+
As in the case of `input` and `output`:
|
390
|
+
|
391
|
+
##### At session start:
|
392
|
+
|
393
|
+
Pry.start(self, :commands => MyCommands)
|
394
|
+
|
395
|
+
##### At runtime:
|
396
|
+
|
397
|
+
_pry_.commands = MyCommands
|
398
|
+
|
399
|
+
#### The command API
|
400
|
+
|
401
|
+
The command API is defined by the `Pry::CommandBase` class (hence why
|
402
|
+
all commands must inherit from it or a subclass). The API works as follows:
|
403
|
+
|
404
|
+
##### `command` method
|
405
|
+
|
406
|
+
The `command` method defines a new command, its parameter is the
|
407
|
+
name of the command and an optional second parameter is a description of
|
408
|
+
the command.
|
409
|
+
|
410
|
+
The associated block defines the action to be performed. The number of
|
411
|
+
parameters in the block determine the number of parameters that will
|
412
|
+
be sent to the command (from the Pry prompt) when it is invoked. Note
|
413
|
+
that all parameters that are received will be strings; if a parameter
|
414
|
+
is not received it will be set to `nil`.
|
415
|
+
|
416
|
+
command "hello" do |x, y, z|
|
417
|
+
puts "hello there #{x}, #{y}, and #{z}!"
|
418
|
+
end
|
419
|
+
|
420
|
+
Command aliases can also be defined - simply use an array of strings
|
421
|
+
for the command name - all these strings will be valid names for the
|
422
|
+
command.
|
423
|
+
|
424
|
+
command ["ls", "dir"], "show a list of local vars" do
|
425
|
+
output.puts target.eval("local_variables")
|
426
|
+
end
|
427
|
+
|
428
|
+
##### `delete` method
|
429
|
+
|
430
|
+
The `delete` method deletes a command or a group of a commands; it
|
431
|
+
can be useful when inheriting from another command set when you decide
|
432
|
+
to keep only a portion of inherited commands.
|
433
|
+
|
434
|
+
class MyCommands < Pry::Commands
|
435
|
+
delete "show_method", "show_imethod"
|
436
|
+
end
|
437
|
+
|
438
|
+
##### `import_from` method
|
439
|
+
|
440
|
+
The `import_from` method enables you to specifically select which
|
441
|
+
commands will be copied across from another command set, useful when
|
442
|
+
you only want a small number of commands and so inheriting and then
|
443
|
+
deleting would be inefficient. The first parameter to `import_from`
|
444
|
+
is the class to import from and the other paramters are the names of
|
445
|
+
the commands to import:
|
446
|
+
|
447
|
+
class MyCommands < Pry::CommandBase
|
448
|
+
import_from Pry::Commands, "ls", "status", "!"
|
449
|
+
end
|
450
|
+
|
451
|
+
##### `run` method
|
452
|
+
|
453
|
+
The `run` command invokes one command from within another.
|
454
|
+
The first parameter is the name of the command to invoke
|
455
|
+
and the remainder of the parameters will be passed on to the command
|
456
|
+
being invoked:
|
457
|
+
|
458
|
+
class MyCommands < Pry::Commands
|
459
|
+
command "ls_with_hello" do
|
460
|
+
output.puts "hello!"
|
461
|
+
run "ls"
|
462
|
+
end
|
463
|
+
end
|
464
|
+
|
465
|
+
#### Utility methods for commands
|
466
|
+
|
467
|
+
All commands can access the special `output` and `target` methods. The
|
468
|
+
`output` method returns the `output` object for the active pry session.
|
469
|
+
Ensuring that your commands invoke `puts` on this rather than using
|
470
|
+
the top-level `puts` will ensure that all your session output goes to
|
471
|
+
the same place.
|
472
|
+
|
473
|
+
The `target` method returns the `Binding` object the Pry session is currently
|
474
|
+
active on - useful when your commands need to manipulate or examine
|
475
|
+
the state of the object. E.g, the "ls" command is implemented as follows
|
476
|
+
|
477
|
+
command "ls" do
|
478
|
+
output.puts target.eval("local_variables + instance_variables").inspect
|
479
|
+
end
|
480
|
+
|
481
|
+
#### The opts hash
|
482
|
+
|
483
|
+
These are miscellaneous variables that may be useful to your commands:
|
484
|
+
|
485
|
+
* `opts[:val]` - The line of input that invoked the command.
|
486
|
+
* `opts[:eval_string]` - The cumulative lines of input for multi-line input.
|
487
|
+
* `opts[:nesting]` - Lowlevel session nesting information.
|
488
|
+
* `opts[:commands]` - Lowlevel data of all Pry commands.
|
489
|
+
|
490
|
+
(see commands.rb for examples of how some of these options are used)
|
491
|
+
|
492
|
+
#### The `help` command
|
493
|
+
|
494
|
+
The `Pry::CommandBase` class automatically defines a `help` command
|
495
|
+
for you. Typing `help` in a Pry session will show a list of commands
|
496
|
+
to the user followed by their descriptions. Passing a parameter to
|
497
|
+
`help` with the command name will just return the description of that
|
498
|
+
specific command. If a description is left out it will automatically
|
499
|
+
be given the description "No description.".
|
500
|
+
|
501
|
+
If the description is explicitly set to `""` then this command will
|
502
|
+
not be displayed in `help`.
|
503
|
+
|
504
|
+
### Hooks
|
505
|
+
|
506
|
+
Currently Pry supports just two hooks: `before_session` and
|
507
|
+
`after_session`. These hooks are invoked before a Pry session starts
|
508
|
+
and after a session ends respectively. The default hooks used are
|
509
|
+
stored in the `Pry::DEFAULT_HOOKS` and just output the text `"Beginning
|
510
|
+
Pry session for <obj>"` and `"Ending Pry session for <obj>"`.
|
511
|
+
|
512
|
+
#### Example: Setting global hooks
|
513
|
+
|
514
|
+
All subsequent Pry instances will use these hooks as default:
|
515
|
+
|
516
|
+
Pry.hooks = {
|
517
|
+
:before_session => proc { |out, obj| out.puts "Opened #{obj}" },
|
518
|
+
:after_session => proc { |out, obj| out.puts "Closed #{obj}" }
|
519
|
+
}
|
520
|
+
|
521
|
+
5.pry
|
522
|
+
|
523
|
+
Inside the session:
|
524
|
+
|
525
|
+
Opened 5
|
526
|
+
pry(5)> exit
|
527
|
+
Closed 5
|
528
|
+
|
529
|
+
Note that the `before_session` and `after_session` procs receive the
|
530
|
+
current session's output object and session receiver as parameters.
|
531
|
+
|
532
|
+
#### Example: Setting hooks for a specific session
|
533
|
+
|
534
|
+
Like all the other customization options, the global default (as
|
535
|
+
explained above) can be overriden for a specific session, either at
|
536
|
+
session start or during runtime.
|
537
|
+
|
538
|
+
##### At session start
|
539
|
+
|
540
|
+
Pry.start(self, :hooks => { :before_session => proc { puts "hello world!" },
|
541
|
+
:after_session => proc { puts "goodbye world!" }
|
542
|
+
})
|
543
|
+
|
544
|
+
##### At runtime
|
545
|
+
|
546
|
+
_pry_.hooks = { :before_session => proc { puts "puts "hello world!" } }
|
547
|
+
|
548
|
+
### Prompts
|
549
|
+
|
550
|
+
The Pry prompt is used by `Readline` and other input objects that
|
551
|
+
accept a prompt. Pry can accept two prompt-types for every prompt; the
|
552
|
+
'main prompt' and the 'wait prompt'. The main prompt is always used
|
553
|
+
for the first line of input; the wait prompt is used in multi-line
|
554
|
+
input to indicate that the current expression is incomplete and more lines of
|
555
|
+
input are required. The default Prompt used by Pry is stored in the
|
556
|
+
`Pry::DEFAULT_PROMPT` constant.
|
557
|
+
|
558
|
+
A valid Pry prompt is either a single `Proc` object or a two element
|
559
|
+
array of `Proc` objects. When an array is used the first element is
|
560
|
+
the 'main prompt' and the last element is the 'wait prompt'. When a
|
561
|
+
single `Proc` object is used it will be used for both the main prompt
|
562
|
+
and the wait prompt.
|
563
|
+
|
564
|
+
#### Example: Setting global prompt
|
565
|
+
|
566
|
+
The prompt `Proc` objects are passed the receiver of the Pry session
|
567
|
+
and the nesting level of that session as parameters (they can simply
|
568
|
+
ignore these if they do not need them).
|
569
|
+
|
570
|
+
# Using one proc for both main and wait prompts
|
571
|
+
Pry.prompt = proc { |obj, nest_level| "#{obj}:#{nest_level}> " }
|
572
|
+
|
573
|
+
# Alternatively, provide two procs; one for main and one for wait
|
574
|
+
Pry.prompt = [ proc { "ENTER INPUT> " }, proc { "MORE INPUT REQUIRED!* " }]
|
575
|
+
|
576
|
+
#### Example: Setting the prompt for a specific session
|
577
|
+
|
578
|
+
##### At session start
|
579
|
+
|
580
|
+
Pry.start(self, :prompt => [proc { "ENTER INPUT> " },
|
581
|
+
proc { "MORE INPUT REQUIRED!* " }])
|
582
|
+
|
583
|
+
##### At runtime
|
584
|
+
|
585
|
+
_pry_.prompt = [proc { "ENTER INPUT> " },
|
586
|
+
proc { "MORE INPUT REQUIRED!* " }]
|
587
|
+
|
588
|
+
### Print
|
589
|
+
|
590
|
+
The Print phase of Pry's READ-EVAL-PRINT-LOOP can be customized. The
|
591
|
+
default action is stored in the `Pry::DEFAULT_PRINT` constant and it
|
592
|
+
simply outputs the value of the current expression preceded by a `=>` (or the first
|
593
|
+
line of the backtrace if the value is an `Exception` object.)
|
594
|
+
|
595
|
+
The print object should be a `Proc` and the parameters passed to the
|
596
|
+
`Proc` are the output object for the current session and the 'value'
|
597
|
+
returned by the current expression.
|
598
|
+
|
599
|
+
#### Example: Setting global print object
|
600
|
+
|
601
|
+
Let's define a print object that displays the full backtrace of any
|
602
|
+
exception and precedes the output of a value by the text `"Output is: "`:
|
603
|
+
|
604
|
+
Pry.print = proc do |output, value|
|
605
|
+
case value
|
606
|
+
when Exception
|
607
|
+
output.puts value.backtrace
|
608
|
+
else
|
609
|
+
output.puts "Output is: #{value}"
|
610
|
+
end
|
611
|
+
end
|
612
|
+
|
613
|
+
#### Example: Setting the print object for a specific session
|
614
|
+
|
615
|
+
##### At session start
|
616
|
+
|
617
|
+
Pry.start(self, :print => proc do |output, value|
|
618
|
+
case value
|
619
|
+
when Exception
|
620
|
+
output.puts value.backtrace
|
621
|
+
else
|
622
|
+
output.puts "Output is: #{value.inspect}"
|
623
|
+
end
|
624
|
+
end)
|
625
|
+
|
626
|
+
##### At runtime
|
627
|
+
|
628
|
+
_pry_.print = proc do |output, value|
|
629
|
+
case value
|
630
|
+
when Exception
|
631
|
+
output.puts value.backtrace
|
632
|
+
else
|
633
|
+
output.puts "Output is: #{value.inspect}"
|
634
|
+
end
|
635
|
+
end
|
636
|
+
|
225
637
|
Contact
|
226
638
|
-------
|
227
639
|
|
@@ -229,3 +641,4 @@ Problems or questions contact me at [github](http://github.com/banister)
|
|
229
641
|
|
230
642
|
|
231
643
|
|
644
|
+
|