pry 0.9.12.2 → 0.14.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +1141 -0
- data/LICENSE +2 -2
- data/README.md +466 -0
- data/bin/pry +4 -7
- data/lib/pry/basic_object.rb +10 -0
- data/lib/pry/block_command.rb +22 -0
- data/lib/pry/class_command.rb +194 -0
- data/lib/pry/cli.rb +97 -92
- data/lib/pry/code/code_file.rb +114 -0
- data/lib/pry/code/code_range.rb +7 -4
- data/lib/pry/code/loc.rb +27 -14
- data/lib/pry/code.rb +62 -90
- data/lib/pry/code_object.rb +83 -39
- data/lib/pry/color_printer.rb +66 -0
- data/lib/pry/command.rb +202 -371
- data/lib/pry/command_set.rb +151 -133
- data/lib/pry/command_state.rb +31 -0
- data/lib/pry/commands/amend_line.rb +86 -82
- data/lib/pry/commands/bang.rb +18 -14
- data/lib/pry/commands/bang_pry.rb +15 -11
- data/lib/pry/commands/cat/abstract_formatter.rb +23 -18
- data/lib/pry/commands/cat/exception_formatter.rb +85 -73
- data/lib/pry/commands/cat/file_formatter.rb +56 -63
- data/lib/pry/commands/cat/input_expression_formatter.rb +35 -30
- data/lib/pry/commands/cat.rb +64 -47
- data/lib/pry/commands/cd.rb +42 -26
- data/lib/pry/commands/change_inspector.rb +34 -0
- data/lib/pry/commands/change_prompt.rb +51 -0
- data/lib/pry/commands/clear_screen.rb +20 -0
- data/lib/pry/commands/code_collector.rb +148 -131
- data/lib/pry/commands/disable_pry.rb +23 -19
- data/lib/pry/commands/easter_eggs.rb +23 -34
- data/lib/pry/commands/edit/exception_patcher.rb +21 -17
- data/lib/pry/commands/edit/file_and_line_locator.rb +33 -24
- data/lib/pry/commands/edit.rb +183 -167
- data/lib/pry/commands/exit.rb +40 -35
- data/lib/pry/commands/exit_all.rb +24 -20
- data/lib/pry/commands/exit_program.rb +20 -17
- data/lib/pry/commands/find_method.rb +167 -167
- data/lib/pry/commands/fix_indent.rb +16 -12
- data/lib/pry/commands/help.rb +140 -133
- data/lib/pry/commands/hist.rb +153 -132
- data/lib/pry/commands/import_set.rb +20 -15
- data/lib/pry/commands/jump_to.rb +25 -21
- data/lib/pry/commands/list_inspectors.rb +42 -0
- data/lib/pry/commands/ls/constants.rb +75 -0
- data/lib/pry/commands/ls/formatter.rb +55 -0
- data/lib/pry/commands/ls/globals.rb +50 -0
- data/lib/pry/commands/ls/grep.rb +23 -0
- data/lib/pry/commands/ls/instance_vars.rb +40 -0
- data/lib/pry/commands/ls/interrogatable.rb +24 -0
- data/lib/pry/commands/ls/jruby_hacks.rb +55 -0
- data/lib/pry/commands/ls/local_names.rb +37 -0
- data/lib/pry/commands/ls/local_vars.rb +47 -0
- data/lib/pry/commands/ls/ls_entity.rb +65 -0
- data/lib/pry/commands/ls/methods.rb +55 -0
- data/lib/pry/commands/ls/methods_helper.rb +50 -0
- data/lib/pry/commands/ls/self_methods.rb +34 -0
- data/lib/pry/commands/ls.rb +100 -303
- data/lib/pry/commands/nesting.rb +21 -17
- data/lib/pry/commands/play.rb +93 -49
- data/lib/pry/commands/pry_backtrace.rb +22 -18
- data/lib/pry/commands/pry_version.rb +15 -11
- data/lib/pry/commands/raise_up.rb +33 -27
- data/lib/pry/commands/reload_code.rb +57 -48
- data/lib/pry/commands/reset.rb +16 -12
- data/lib/pry/commands/ri.rb +57 -38
- data/lib/pry/commands/save_file.rb +45 -43
- data/lib/pry/commands/shell_command.rb +66 -34
- data/lib/pry/commands/shell_mode.rb +22 -20
- data/lib/pry/commands/show_doc.rb +80 -65
- data/lib/pry/commands/show_info.rb +193 -159
- data/lib/pry/commands/show_input.rb +16 -11
- data/lib/pry/commands/show_source.rb +113 -33
- data/lib/pry/commands/stat.rb +35 -31
- data/lib/pry/commands/switch_to.rb +21 -15
- data/lib/pry/commands/toggle_color.rb +21 -13
- data/lib/pry/commands/watch_expression/expression.rb +43 -0
- data/lib/pry/commands/watch_expression.rb +110 -0
- data/lib/pry/commands/whereami.rb +157 -134
- data/lib/pry/commands/wtf.rb +78 -40
- data/lib/pry/config/attributable.rb +22 -0
- data/lib/pry/config/lazy_value.rb +29 -0
- data/lib/pry/config/memoized_value.rb +34 -0
- data/lib/pry/config/value.rb +24 -0
- data/lib/pry/config.rb +290 -220
- data/lib/pry/control_d_handler.rb +28 -0
- data/lib/pry/core_extensions.rb +50 -27
- data/lib/pry/editor.rb +130 -102
- data/lib/pry/env.rb +18 -0
- data/lib/pry/exception_handler.rb +43 -0
- data/lib/pry/exceptions.rb +73 -0
- data/lib/pry/forwardable.rb +27 -0
- data/lib/pry/helpers/base_helpers.rb +22 -151
- data/lib/pry/helpers/command_helpers.rb +55 -63
- data/lib/pry/helpers/documentation_helpers.rb +21 -13
- data/lib/pry/helpers/options_helpers.rb +15 -8
- data/lib/pry/helpers/platform.rb +55 -0
- data/lib/pry/helpers/table.rb +44 -32
- data/lib/pry/helpers/text.rb +96 -86
- data/lib/pry/helpers.rb +3 -0
- data/lib/pry/history.rb +101 -70
- data/lib/pry/hooks.rb +67 -137
- data/lib/pry/indent.rb +79 -73
- data/lib/pry/input_completer.rb +283 -0
- data/lib/pry/input_lock.rb +129 -0
- data/lib/pry/inspector.rb +39 -0
- data/lib/pry/last_exception.rb +61 -0
- data/lib/pry/method/disowned.rb +19 -5
- data/lib/pry/{commands/edit/method_patcher.rb → method/patcher.rb} +51 -42
- data/lib/pry/method/weird_method_locator.rb +80 -44
- data/lib/pry/method.rb +225 -176
- data/lib/pry/object_path.rb +91 -0
- data/lib/pry/output.rb +136 -0
- data/lib/pry/pager.rb +227 -68
- data/lib/pry/prompt.rb +214 -0
- data/lib/pry/pry_class.rb +216 -289
- data/lib/pry/pry_instance.rb +438 -500
- data/lib/pry/repl.rb +256 -0
- data/lib/pry/repl_file_loader.rb +34 -35
- data/lib/pry/ring.rb +89 -0
- data/lib/pry/slop/LICENSE +20 -0
- data/lib/pry/slop/commands.rb +190 -0
- data/lib/pry/slop/option.rb +210 -0
- data/lib/pry/slop.rb +672 -0
- data/lib/pry/syntax_highlighter.rb +26 -0
- data/lib/pry/system_command_handler.rb +17 -0
- data/lib/pry/testable/evalable.rb +24 -0
- data/lib/pry/testable/mockable.rb +22 -0
- data/lib/pry/testable/pry_tester.rb +88 -0
- data/lib/pry/testable/utility.rb +34 -0
- data/lib/pry/testable/variables.rb +52 -0
- data/lib/pry/testable.rb +68 -0
- data/lib/pry/version.rb +3 -1
- data/lib/pry/warning.rb +20 -0
- data/lib/pry/{module_candidate.rb → wrapped_module/candidate.rb} +36 -43
- data/lib/pry/wrapped_module.rb +102 -103
- data/lib/pry.rb +135 -261
- metadata +94 -283
- data/.document +0 -2
- data/.gitignore +0 -16
- data/.travis.yml +0 -21
- data/.yardopts +0 -1
- data/CHANGELOG +0 -534
- data/CONTRIBUTORS +0 -55
- data/Gemfile +0 -9
- data/Guardfile +0 -62
- data/README.markdown +0 -400
- data/Rakefile +0 -140
- data/TODO +0 -117
- data/lib/pry/commands/disabled_commands.rb +0 -2
- data/lib/pry/commands/gem_cd.rb +0 -26
- data/lib/pry/commands/gem_install.rb +0 -29
- data/lib/pry/commands/gem_list.rb +0 -33
- data/lib/pry/commands/gem_open.rb +0 -29
- data/lib/pry/commands/gist.rb +0 -102
- data/lib/pry/commands/install_command.rb +0 -51
- data/lib/pry/commands/simple_prompt.rb +0 -22
- data/lib/pry/commands.rb +0 -6
- data/lib/pry/completion.rb +0 -304
- data/lib/pry/custom_completions.rb +0 -6
- data/lib/pry/history_array.rb +0 -116
- data/lib/pry/plugins.rb +0 -103
- data/lib/pry/rbx_method.rb +0 -13
- data/lib/pry/rbx_path.rb +0 -22
- data/lib/pry/rubygem.rb +0 -74
- data/lib/pry/terminal.rb +0 -78
- data/lib/pry/test/helper.rb +0 -185
- data/man/pry.1 +0 -195
- data/man/pry.1.html +0 -204
- data/man/pry.1.ronn +0 -141
- data/pry.gemspec +0 -30
- data/spec/Procfile +0 -3
- data/spec/cli_spec.rb +0 -78
- data/spec/code_object_spec.rb +0 -277
- data/spec/code_spec.rb +0 -219
- data/spec/command_helpers_spec.rb +0 -29
- data/spec/command_integration_spec.rb +0 -644
- data/spec/command_set_spec.rb +0 -627
- data/spec/command_spec.rb +0 -821
- data/spec/commands/amend_line_spec.rb +0 -247
- data/spec/commands/bang_spec.rb +0 -19
- data/spec/commands/cat_spec.rb +0 -164
- data/spec/commands/cd_spec.rb +0 -250
- data/spec/commands/disable_pry_spec.rb +0 -25
- data/spec/commands/edit_spec.rb +0 -727
- data/spec/commands/exit_all_spec.rb +0 -34
- data/spec/commands/exit_program_spec.rb +0 -19
- data/spec/commands/exit_spec.rb +0 -34
- data/spec/commands/find_method_spec.rb +0 -70
- data/spec/commands/gem_list_spec.rb +0 -26
- data/spec/commands/gist_spec.rb +0 -79
- data/spec/commands/help_spec.rb +0 -56
- data/spec/commands/hist_spec.rb +0 -181
- data/spec/commands/jump_to_spec.rb +0 -15
- data/spec/commands/ls_spec.rb +0 -181
- data/spec/commands/play_spec.rb +0 -140
- data/spec/commands/raise_up_spec.rb +0 -56
- data/spec/commands/save_file_spec.rb +0 -177
- data/spec/commands/show_doc_spec.rb +0 -510
- data/spec/commands/show_input_spec.rb +0 -17
- data/spec/commands/show_source_spec.rb +0 -782
- data/spec/commands/whereami_spec.rb +0 -203
- data/spec/completion_spec.rb +0 -239
- data/spec/control_d_handler_spec.rb +0 -58
- data/spec/documentation_helper_spec.rb +0 -73
- data/spec/editor_spec.rb +0 -79
- data/spec/exception_whitelist_spec.rb +0 -21
- data/spec/fixtures/candidate_helper1.rb +0 -11
- data/spec/fixtures/candidate_helper2.rb +0 -8
- data/spec/fixtures/example.erb +0 -5
- data/spec/fixtures/example_nesting.rb +0 -33
- data/spec/fixtures/show_source_doc_examples.rb +0 -15
- data/spec/fixtures/testrc +0 -2
- data/spec/fixtures/testrcbad +0 -2
- data/spec/fixtures/whereami_helper.rb +0 -6
- data/spec/helper.rb +0 -34
- data/spec/helpers/bacon.rb +0 -86
- data/spec/helpers/mock_pry.rb +0 -43
- data/spec/helpers/table_spec.rb +0 -105
- data/spec/history_array_spec.rb +0 -67
- data/spec/hooks_spec.rb +0 -522
- data/spec/indent_spec.rb +0 -301
- data/spec/input_stack_spec.rb +0 -90
- data/spec/method_spec.rb +0 -482
- data/spec/prompt_spec.rb +0 -60
- data/spec/pry_defaults_spec.rb +0 -419
- data/spec/pry_history_spec.rb +0 -99
- data/spec/pry_output_spec.rb +0 -95
- data/spec/pry_spec.rb +0 -504
- data/spec/run_command_spec.rb +0 -25
- data/spec/sticky_locals_spec.rb +0 -157
- data/spec/syntax_checking_spec.rb +0 -81
- data/spec/wrapped_module_spec.rb +0 -261
- data/wiki/Customizing-pry.md +0 -397
- data/wiki/Home.md +0 -4
data/LICENSE
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
License
|
2
2
|
-------
|
3
3
|
|
4
|
-
(The MIT License)
|
4
|
+
(The MIT License)
|
5
5
|
|
6
|
-
Copyright (c)
|
6
|
+
Copyright (c) 2018 John Mair (banisterfiend)
|
7
7
|
|
8
8
|
Permission is hereby granted, free of charge, to any person obtaining
|
9
9
|
a copy of this software and associated documentation files (the
|
data/README.md
ADDED
@@ -0,0 +1,466 @@
|
|
1
|
+
Pry
|
2
|
+
===
|
3
|
+
|
4
|
+
[![Pry Build Status](https://github.com/pry/pry/workflows/pry/badge.svg)](https://github.com/pry/pry/actions)
|
5
|
+
[![Code Climate](https://codeclimate.com/github/pry/pry.svg)](https://codeclimate.com/github/pry/pry)
|
6
|
+
[![Gem Version](https://badge.fury.io/rb/pry.svg)](https://badge.fury.io/rb/pry)
|
7
|
+
[![Documentation Status](https://inch-ci.org/github/pry/pry.svg?branch=master)](https://inch-ci.org/github/pry/pry)
|
8
|
+
[![Downloads](https://img.shields.io/gem/dt/pry.svg?style=flat)](https://rubygems.org/gems/pry)
|
9
|
+
|
10
|
+
![Pry logo](https://www.dropbox.com/s/zp8o63kquby2rln/pry_logo_350.png?raw=1)
|
11
|
+
|
12
|
+
© John Mair ([banisterfiend](https://twitter.com/banisterfiend)) 2018<br> (Creator)
|
13
|
+
|
14
|
+
© Kyrylo Silin ([kyrylosilin](https://twitter.com/kyrylosilin)) 2018<br> (Maintainer)
|
15
|
+
|
16
|
+
**Alumni:**
|
17
|
+
|
18
|
+
* Conrad Irwin
|
19
|
+
* Ryan Fitzgerald
|
20
|
+
* Robert Gleeson
|
21
|
+
|
22
|
+
**Links:**
|
23
|
+
|
24
|
+
* https://pry.github.io/
|
25
|
+
* [YARD API documentation](https://www.rubydoc.info/gems/pry)
|
26
|
+
* [Wiki](https://github.com/pry/pry/wiki)
|
27
|
+
|
28
|
+
Table of Contents
|
29
|
+
=================
|
30
|
+
|
31
|
+
* [Introduction](#introduction)
|
32
|
+
* [Key features](#key-features)
|
33
|
+
* [Installation](#installation)
|
34
|
+
* [Overview](#overview)
|
35
|
+
* [Commands](#commands)
|
36
|
+
* [Navigating around state](#navigating-around-state)
|
37
|
+
* [Runtime invocation](#runtime-invocation)
|
38
|
+
* [Command Shell Integration](#command-shell-integration)
|
39
|
+
* [Code Browsing](#code-browsing)
|
40
|
+
* [Documentation Browsing](#documentation-browsing)
|
41
|
+
* [Edit methods](#edit-methods)
|
42
|
+
* [Live Help System](#live-help-system)
|
43
|
+
* [Use Pry as your Rails Console](#use-pry-as-your-rails-console)
|
44
|
+
* [Syntax Highlighting](#syntax-highlighting)
|
45
|
+
* [Supported Rubies](#supported-rubies)
|
46
|
+
* [Contact](#contact)
|
47
|
+
* [License](#license)
|
48
|
+
* [Contributors](#contributors)
|
49
|
+
|
50
|
+
Introduction
|
51
|
+
------------
|
52
|
+
|
53
|
+
Pry is a runtime developer console and IRB alternative with powerful
|
54
|
+
introspection capabilities. Pry aims to be more than an IRB replacement. It is
|
55
|
+
an attempt to bring REPL driven programming to the Ruby language.
|
56
|
+
|
57
|
+
Key features
|
58
|
+
------------
|
59
|
+
|
60
|
+
* Source code browsing (including core C source with the pry-doc gem)
|
61
|
+
* Documentation browsing
|
62
|
+
* Live help system
|
63
|
+
* Open methods in editors (`edit Class#method`)
|
64
|
+
* Syntax highlighting
|
65
|
+
* Command shell integration (start editors, run git, and rake from within Pry)
|
66
|
+
* Gist integration
|
67
|
+
* Navigation around state (`cd`, `ls` and friends)
|
68
|
+
* Runtime invocation (use Pry as a developer console or debugger)
|
69
|
+
* Exotic object support (BasicObject instances, IClasses, ...)
|
70
|
+
* A powerful and flexible command system
|
71
|
+
* Ability to view and replay history
|
72
|
+
* Many convenience commands inspired by IPython, Smalltalk and other advanced
|
73
|
+
REPLs
|
74
|
+
* A wide-range number of
|
75
|
+
[plugins](https://github.com/pry/pry/wiki/Available-plugins) that provide
|
76
|
+
remote sessions, full debugging functionality, and more.
|
77
|
+
|
78
|
+
Installation
|
79
|
+
------------
|
80
|
+
|
81
|
+
### Bundler
|
82
|
+
|
83
|
+
```ruby
|
84
|
+
gem 'pry', '~> 0.13.1'
|
85
|
+
```
|
86
|
+
|
87
|
+
### Manual
|
88
|
+
|
89
|
+
```sh
|
90
|
+
gem install pry
|
91
|
+
```
|
92
|
+
|
93
|
+
Overview
|
94
|
+
--------
|
95
|
+
|
96
|
+
Pry is fairly flexible and allows significant user
|
97
|
+
[customization](https://github.com/pry/pry/wiki/Customization-and-configuration).
|
98
|
+
It is trivial to read from any object that has a `readline` method and
|
99
|
+
write to any object that has a `puts` method. Many other aspects of Pry are
|
100
|
+
also configurable, making it a good choice for implementing custom shells.
|
101
|
+
|
102
|
+
Pry comes with an executable so it can be invoked at the command line. Just
|
103
|
+
enter `pry` to start. A `pryrc` file in `$XDG_CONFIG_HOME/pry/` or the user's
|
104
|
+
home directory will be loaded if it exists. Type `pry --help` at the command
|
105
|
+
line for more information.
|
106
|
+
|
107
|
+
### Commands
|
108
|
+
|
109
|
+
Nearly every piece of functionality in a Pry session is implemented as a
|
110
|
+
command. Commands are not methods and must start at the beginning of a line,
|
111
|
+
with no whitespace in between. Commands support a flexible syntax and allow
|
112
|
+
'options' in the same way as shell commands, for example the following Pry
|
113
|
+
command will show a list of all private instance methods (in scope) that begin
|
114
|
+
with 'pa'
|
115
|
+
|
116
|
+
```ruby
|
117
|
+
pry(YARD::Parser::SourceParser):5> ls -Mp --grep ^pa
|
118
|
+
YARD::Parser::SourceParser#methods: parse parser_class parser_type parser_type= parser_type_for_filename
|
119
|
+
```
|
120
|
+
|
121
|
+
### Navigating around state
|
122
|
+
|
123
|
+
Pry allows us to pop in and out of different scopes (objects) using the `cd`
|
124
|
+
command. This enables us to explore the run-time view of a program or
|
125
|
+
library. To view which variables and methods are available within a particular
|
126
|
+
scope we use the versatile [ls
|
127
|
+
command.](https://gist.github.com/c0fc686ef923c8b87715)
|
128
|
+
|
129
|
+
Here we will begin Pry at top-level, then Pry on a class and then on an instance
|
130
|
+
variable inside that class:
|
131
|
+
|
132
|
+
```ruby
|
133
|
+
pry(main)> class Hello
|
134
|
+
pry(main)* @x = 20
|
135
|
+
pry(main)* end
|
136
|
+
=> 20
|
137
|
+
pry(main)> cd Hello
|
138
|
+
pry(Hello):1> ls -i
|
139
|
+
instance variables: @x
|
140
|
+
pry(Hello):1> cd @x
|
141
|
+
pry(20):2> self + 10
|
142
|
+
=> 30
|
143
|
+
pry(20):2> cd ..
|
144
|
+
pry(Hello):1> cd ..
|
145
|
+
pry(main)> cd ..
|
146
|
+
```
|
147
|
+
|
148
|
+
The number after the `:` in the pry prompt indicates the nesting level. To
|
149
|
+
display more information about nesting, use the `nesting` command. E.g
|
150
|
+
|
151
|
+
```ruby
|
152
|
+
pry("friend"):3> nesting
|
153
|
+
Nesting status:
|
154
|
+
0. main (Pry top level)
|
155
|
+
1. Hello
|
156
|
+
2. 100
|
157
|
+
3. "friend"
|
158
|
+
=> nil
|
159
|
+
```
|
160
|
+
|
161
|
+
We can then jump back to any of the previous nesting levels by using the
|
162
|
+
`jump-to` command:
|
163
|
+
|
164
|
+
```ruby
|
165
|
+
pry("friend"):3> jump-to 1
|
166
|
+
=> 100
|
167
|
+
pry(Hello):1>
|
168
|
+
```
|
169
|
+
|
170
|
+
### Runtime invocation
|
171
|
+
|
172
|
+
Pry can be invoked in the middle of a running program. It opens a Pry session at
|
173
|
+
the point it's called and makes all program state at that point available. It
|
174
|
+
can be invoked on any object using the `my_object.pry` syntax or on the current
|
175
|
+
binding (or any binding) using `binding.pry`. The Pry session will then begin
|
176
|
+
within the scope of the object (or binding). When the session ends the program
|
177
|
+
continues with any modifications you made to it.
|
178
|
+
|
179
|
+
This functionality can be used for such things as: debugging, implementing
|
180
|
+
developer consoles and applying hot patches.
|
181
|
+
|
182
|
+
code:
|
183
|
+
|
184
|
+
```ruby
|
185
|
+
# test.rb
|
186
|
+
require 'pry'
|
187
|
+
|
188
|
+
class A
|
189
|
+
def hello() puts "hello world!" end
|
190
|
+
end
|
191
|
+
|
192
|
+
a = A.new
|
193
|
+
|
194
|
+
# start a REPL session
|
195
|
+
binding.pry
|
196
|
+
|
197
|
+
# program resumes here (after pry session)
|
198
|
+
puts "program resumes here."
|
199
|
+
```
|
200
|
+
|
201
|
+
Pry session:
|
202
|
+
|
203
|
+
```ruby
|
204
|
+
pry(main)> a.hello
|
205
|
+
hello world!
|
206
|
+
=> nil
|
207
|
+
pry(main)> def a.goodbye
|
208
|
+
pry(main)* puts "goodbye cruel world!"
|
209
|
+
pry(main)* end
|
210
|
+
=> :goodbye
|
211
|
+
pry(main)> a.goodbye
|
212
|
+
goodbye cruel world!
|
213
|
+
=> nil
|
214
|
+
pry(main)> exit
|
215
|
+
|
216
|
+
program resumes here.
|
217
|
+
```
|
218
|
+
|
219
|
+
### Command Shell Integration
|
220
|
+
|
221
|
+
A line of input that begins with a '.' will be forwarded to the command
|
222
|
+
shell. This enables us to navigate the file system, spawn editors, and run git
|
223
|
+
and rake directly from within Pry.
|
224
|
+
|
225
|
+
Further, we can use the `shell-mode` command to incorporate the present working
|
226
|
+
directory into the Pry prompt and bring in (limited at this stage, sorry) file
|
227
|
+
name completion. We can also interpolate Ruby code directly into the shell by
|
228
|
+
using the normal `#{}` string interpolation syntax.
|
229
|
+
|
230
|
+
In the code below we're going to switch to `shell-mode` and edit the `pryrc`
|
231
|
+
file. We'll then cat its contents and reload the file.
|
232
|
+
|
233
|
+
```ruby
|
234
|
+
pry(main)> shell-mode
|
235
|
+
pry main:/home/john/ruby/projects/pry $ .cd ~
|
236
|
+
pry main:/home/john $ .emacsclient .pryrc
|
237
|
+
pry main:/home/john $ .cat .pryrc
|
238
|
+
def hello_world
|
239
|
+
puts "hello world!"
|
240
|
+
end
|
241
|
+
pry main:/home/john $ load ".pryrc"
|
242
|
+
=> true
|
243
|
+
pry main:/home/john $ hello_world
|
244
|
+
hello world!
|
245
|
+
```
|
246
|
+
|
247
|
+
We can also interpolate Ruby code into the shell. In the example below we use
|
248
|
+
the shell command `cat` on a random file from the current directory and count
|
249
|
+
the number of lines in that file with `wc`:
|
250
|
+
|
251
|
+
```ruby
|
252
|
+
pry main:/home/john $ .cat #{Dir['*.*'].sample} | wc -l
|
253
|
+
44
|
254
|
+
```
|
255
|
+
|
256
|
+
### Code Browsing
|
257
|
+
|
258
|
+
You can browse method source code with the `show-source` command. Nearly all
|
259
|
+
Ruby methods (and some C methods, with the pry-doc gem) can have their source
|
260
|
+
viewed. Code that is longer than a page is sent through a pager (such as less),
|
261
|
+
and all code is properly syntax highlighted (even C code).
|
262
|
+
|
263
|
+
The `show-source` command accepts two syntaxes, the typical ri `Class#method`
|
264
|
+
syntax and also simply the name of a method that's in scope. You can optionally
|
265
|
+
pass the `-l` option to `show-source` to include line numbers in the output.
|
266
|
+
|
267
|
+
In the following example we will enter the `Pry` class, list the instance
|
268
|
+
methods beginning with 'se' and display the source code for the `set_last_result` method:
|
269
|
+
|
270
|
+
```ruby
|
271
|
+
pry(main)> cd Pry
|
272
|
+
pry(Pry):1> ls -M --grep se
|
273
|
+
Pry#methods: raise_up raise_up! raise_up_common reset_eval_string select_prompt set_last_result
|
274
|
+
pry(Pry):1> show-source set_last_result -l
|
275
|
+
|
276
|
+
From: /home/john/ruby/projects/pry/lib/pry/pry_instance.rb:405:
|
277
|
+
Owner: Pry
|
278
|
+
Visibility: public
|
279
|
+
Signature: set_last_result(result, code=?)
|
280
|
+
Number of lines: 6
|
281
|
+
|
282
|
+
405: def set_last_result(result, code = "")
|
283
|
+
406: @last_result_is_exception = false
|
284
|
+
407: @output_ring << result
|
285
|
+
408:
|
286
|
+
409: self.last_result = result unless code =~ /\A\s*\z/
|
287
|
+
410: end
|
288
|
+
```
|
289
|
+
|
290
|
+
Note that we can also view C methods (from Ruby Core) using the
|
291
|
+
`pry-doc` plugin; we also show off the alternate syntax for
|
292
|
+
`show-source`:
|
293
|
+
|
294
|
+
```ruby
|
295
|
+
pry(main)> show-source Array#select
|
296
|
+
|
297
|
+
From: array.c in Ruby Core (C Method):
|
298
|
+
Number of lines: 15
|
299
|
+
|
300
|
+
static VALUE
|
301
|
+
rb_ary_select(VALUE ary)
|
302
|
+
{
|
303
|
+
VALUE result;
|
304
|
+
long i;
|
305
|
+
|
306
|
+
RETURN_ENUMERATOR(ary, 0, 0);
|
307
|
+
result = rb_ary_new2(RARRAY_LEN(ary));
|
308
|
+
for (i = 0; i < RARRAY_LEN(ary); i++) {
|
309
|
+
if (RTEST(rb_yield(RARRAY_PTR(ary)[i]))) {
|
310
|
+
rb_ary_push(result, rb_ary_elt(ary, i));
|
311
|
+
}
|
312
|
+
}
|
313
|
+
return result;
|
314
|
+
}
|
315
|
+
```
|
316
|
+
|
317
|
+
### Documentation Browsing
|
318
|
+
|
319
|
+
One use-case for Pry is to explore a program at run-time by `cd`-ing in and out
|
320
|
+
of objects and viewing and invoking methods. In the course of exploring it may
|
321
|
+
be useful to read the documentation for a specific method that you come
|
322
|
+
across. `show-source` command supports two syntaxes - the
|
323
|
+
normal `ri` syntax as well as accepting the name of any method that is currently
|
324
|
+
in scope.
|
325
|
+
|
326
|
+
The Pry documentation system does not rely on pre-generated `rdoc` or `ri`,
|
327
|
+
instead it grabs the comments directly above the method on demand. This results
|
328
|
+
in speedier documentation retrieval and allows the Pry system to retrieve
|
329
|
+
documentation for methods that would not be picked up by `rdoc`. Pry also has a
|
330
|
+
basic understanding of both the rdoc and yard formats and will attempt to syntax
|
331
|
+
highlight the documentation appropriately.
|
332
|
+
|
333
|
+
Nonetheless, the `ri` functionality is very good and has an advantage over Pry's
|
334
|
+
system in that it allows documentation lookup for classes as well as
|
335
|
+
methods. Pry therefore has good integration with `ri` through the `ri`
|
336
|
+
command. The syntax for the command is exactly as it would be in command-line -
|
337
|
+
so it is not necessary to quote strings.
|
338
|
+
|
339
|
+
In our example we will enter the `Gem` class and view the documentation for the
|
340
|
+
`try_activate` method:
|
341
|
+
|
342
|
+
```ruby
|
343
|
+
pry(main)> cd Gem
|
344
|
+
pry(Gem):1> show-source try_activate -d
|
345
|
+
|
346
|
+
From: /Users/john/rbenv/versions/2.7.1/lib/ruby/2.7.0/rubygems.rb:194:
|
347
|
+
Owner: #<Class:Gem>
|
348
|
+
Visibility: public
|
349
|
+
Signature: try_activate(path)
|
350
|
+
Number of lines: 28
|
351
|
+
|
352
|
+
Try to activate a gem containing path. Returns true if
|
353
|
+
activation succeeded or wasn't needed because it was already
|
354
|
+
activated. Returns false if it can't find the path in a gem.
|
355
|
+
|
356
|
+
def self.try_activate(path)
|
357
|
+
# finds the _latest_ version... regardless of loaded specs and their deps
|
358
|
+
# if another gem had a requirement that would mean we shouldn't
|
359
|
+
# activate the latest version, then either it would already be activated
|
360
|
+
# or if it was ambiguous (and thus unresolved) the code in our custom
|
361
|
+
# require will try to activate the more specific version.
|
362
|
+
|
363
|
+
spec = Gem::Specification.find_by_path path
|
364
|
+
pry(Gem):1>
|
365
|
+
```
|
366
|
+
|
367
|
+
We can also use `ri` in the normal way:
|
368
|
+
|
369
|
+
```ruby
|
370
|
+
pry(main) ri Array#each
|
371
|
+
----------------------------------------------------------- Array#each
|
372
|
+
array.each {|item| block } -> array
|
373
|
+
------------------------------------------------------------------------
|
374
|
+
Calls _block_ once for each element in _self_, passing that element
|
375
|
+
as a parameter.
|
376
|
+
|
377
|
+
a = [ "a", "b", "c" ]
|
378
|
+
a.each {|x| print x, " -- " }
|
379
|
+
|
380
|
+
produces:
|
381
|
+
|
382
|
+
a -- b -- c --
|
383
|
+
```
|
384
|
+
|
385
|
+
### Edit methods
|
386
|
+
|
387
|
+
You can use `edit Class#method` or `edit my_method` (if the method is in scope)
|
388
|
+
to open a method for editing directly in your favorite editor. Pry has knowledge
|
389
|
+
of a few different editors and will attempt to open the file at the line the
|
390
|
+
method is defined.
|
391
|
+
|
392
|
+
You can set the editor to use by assigning to the `Pry.editor`
|
393
|
+
accessor. `Pry.editor` will default to `$EDITOR` or failing that will use `nano`
|
394
|
+
as the backup default. The file that is edited will be automatically reloaded
|
395
|
+
after exiting the editor - reloading can be suppressed by passing the
|
396
|
+
`--no-reload` option to `edit`
|
397
|
+
|
398
|
+
In the example below we will set our default editor to "emacsclient" and open
|
399
|
+
the `Pry#repl` method for editing:
|
400
|
+
|
401
|
+
```ruby
|
402
|
+
pry(main)> Pry.editor = "emacsclient"
|
403
|
+
pry(main)> edit Pry#repl
|
404
|
+
```
|
405
|
+
|
406
|
+
### Live Help System
|
407
|
+
|
408
|
+
Many other commands are available in Pry; to see the full list type `help` at
|
409
|
+
the prompt. A short description of each command is provided with basic
|
410
|
+
instructions for use; some commands have a more extensive help that can be
|
411
|
+
accessed via typing `command_name --help`. A command will typically say in its
|
412
|
+
description if the `--help` option is available.
|
413
|
+
|
414
|
+
### Use Pry as your Rails Console
|
415
|
+
|
416
|
+
The recommended way to use Pry as your Rails console is to add [the `pry-rails`
|
417
|
+
gem](https://github.com/rweng/pry-rails) to your Gemfile. This replaces the
|
418
|
+
default console with Pry, in addition to loading the Rails console helpers and
|
419
|
+
adding some useful Rails-specific commands.
|
420
|
+
|
421
|
+
If you don't want to change your Gemfile, you can still run a Pry console in
|
422
|
+
your app's environment using Pry's `-r` flag:
|
423
|
+
|
424
|
+
```sh
|
425
|
+
pry -r ./config/environment
|
426
|
+
```
|
427
|
+
|
428
|
+
Also check out the
|
429
|
+
[wiki](https://github.com/pry/pry/wiki/Setting-up-Rails-or-Heroku-to-use-Pry)
|
430
|
+
for more information about integrating Pry with Rails.
|
431
|
+
|
432
|
+
### Syntax Highlighting
|
433
|
+
|
434
|
+
Syntax highlighting is on by default in Pry. If you want to change the colors,
|
435
|
+
check out the [pry-theme](https://github.com/kyrylo/pry-theme) gem.
|
436
|
+
|
437
|
+
You can toggle the syntax highlighting on and off in a session by using the
|
438
|
+
`toggle-color` command. Alternatively, you can turn it off permanently by
|
439
|
+
putting the line `Pry.color = false` in your `pryrc` file.
|
440
|
+
|
441
|
+
Supported Rubies
|
442
|
+
----------------
|
443
|
+
|
444
|
+
* CRuby >= 2.0.0
|
445
|
+
* JRuby >= 9.0
|
446
|
+
|
447
|
+
Contact
|
448
|
+
-------
|
449
|
+
|
450
|
+
In case you have a problem, question or a bug report, feel free to:
|
451
|
+
|
452
|
+
* ask a question on IRC (#pry on Freenode)
|
453
|
+
* [file an issue](https://github.com/pry/pry/issues)
|
454
|
+
* [tweet at us](https://twitter.com/pryruby)
|
455
|
+
|
456
|
+
License
|
457
|
+
-------
|
458
|
+
|
459
|
+
The project uses the MIT License. See LICENSE.md for details.
|
460
|
+
|
461
|
+
Contributors
|
462
|
+
------------
|
463
|
+
|
464
|
+
Pry is primarily the work of [John Mair (banisterfiend)](https://github.com/banister), for full list
|
465
|
+
of contributors see the
|
466
|
+
[contributors graph](https://github.com/pry/pry/graphs/contributors).
|
data/bin/pry
CHANGED
@@ -1,16 +1,13 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
2
3
|
|
3
4
|
# (C) John Mair (banisterfiend)
|
4
5
|
# MIT license
|
5
6
|
|
6
7
|
$0 = 'pry'
|
7
8
|
|
8
|
-
|
9
|
-
require 'pry'
|
10
|
-
rescue LoadError
|
11
|
-
require 'rubygems'
|
12
|
-
require 'pry'
|
13
|
-
end
|
9
|
+
require 'pry'
|
14
10
|
|
15
11
|
# Process command line options and run Pry
|
16
|
-
Pry::CLI.parse_options
|
12
|
+
opts = Pry::CLI.parse_options
|
13
|
+
Pry::CLI.start(opts)
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Pry
|
4
|
+
# A super-class for Commands that are created with a single block.
|
5
|
+
#
|
6
|
+
# This class ensures that the block is called with the correct number of
|
7
|
+
# arguments and the right context.
|
8
|
+
#
|
9
|
+
# Create subclasses using {Pry::CommandSet#command}.
|
10
|
+
class BlockCommand < Command
|
11
|
+
# Call the block that was registered with this command.
|
12
|
+
# @param [Array<String>] args The arguments passed
|
13
|
+
# @return [Object] The return value of the block
|
14
|
+
def call(*args)
|
15
|
+
instance_exec(*normalize_method_args(block, args), &block)
|
16
|
+
end
|
17
|
+
|
18
|
+
def help
|
19
|
+
"#{command_options[:listing].to_s.ljust(18)} #{description}"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|