OSDN Git Service

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