OSDN Git Service

Regular updates
[twpd/master.git] / pry.md
1 ---
2 title: Pry
3 category: Ruby libraries
4 ---
5
6 ### cd
7
8 ```
9 > cd Array
10 ```
11
12 ```nohighlight
13 > ls
14   Array.methods: [] try_convert
15   Array#methods: & * + abbrev assoc at ...
16 ```
17
18 ```
19 > show-source
20 ```
21
22 ### Code
23
24 ```nohighlight
25 > show-method Array#select
26 ```
27
28 ### Docs
29
30 ```nohighlight
31 > ri Array
32 > ri Array#each
33
34 > cd Gem
35 > show-doc try_activate
36 ```
37
38 ### Finding
39
40 ```nohighlight
41 > find-method each
42   Array#each
43   Array#each_index
44   Enumerable#each_slice
45   ...
46 ```
47
48 ### Editing
49
50     > edit Pry#repl
51
52 ### Gems
53
54     > gem-cd foo      # Switch to gem's dir
55     > gem-install foo
56     > gem-list
57
58 ### Misc commands
59
60     > hist          # History
61     > wtf?          # Trace of recent exception
62
63 ## Rails
64
65 ### Rails console
66
67 Also consider [pry-rails](https://rubygems.org/gems/pry-rails).
68
69     $ pry -r ./config/environment
70
71 ### Rails
72
73     > show-models
74     > show-routes
75     > show-middleware
76
77 ### ls
78
79     > ls         # All
80
81     > ls -m      # Methods
82     > ls -M      # Instance methods
83
84     > ls -g      # Globals
85     > ls -l      # Local vars
86     > ls -c      # Constants
87
88     > ls -i      # Instance vars
89
90     > ls -G xx   # Grey by regex
91
92 ## Shell integration
93
94 shell-mode adds dir to the prompt.
95
96     pry(main)> shell-mode
97     pry(main):/home/x $
98
99 Commands with `.` are shell commands.
100
101     pry(main)> .cat hello.txt
102
103 ## hirb
104 Add the [hirb](https://rubygems.org/gems/hirb) gem.
105
106     > table User.all
107     > view User.all
108     > view User.all, fields: %w[id name email]
109
110 ## pry-rescue
111 Add the [pry-rescue](https://github.com/ConradIrwin/pry-rescue) gem.
112
113 ```rb
114 Pry::rescue {
115   # raise exceptions here
116 }
117 ```
118
119 Or run:
120
121 ```
122 bundle exec rescue rspec
123 ```
124
125 Additional commands:
126
127 ```
128 pry(main)> cd-cause
129 pry(main)> try-again
130 ```
131
132 ## pry-remote
133 Add the [pry-remote](https://github.com/Mon-Ouie/pry-remote) gem.
134
135 ```rb
136 # In your code:
137 binding.remote_pry
138
139 # In the shell:
140 bundle exec pry-remote
141 ```
142
143 ## Reference
144
145  * [Pry](https://github.com/pry/pry)
146  * [Hirb](https://github.com/cldwalker/hirb)