開発の効率を大きく向上してくれるgem Pryをさらに使いこなすための便利なコマンド19個をまとめてみました。標準のコマンドをしっかり理解してPryの使いこなしを目指します!
(03/26 23:05) 『urlヘルパーが生成するpath/urlの確認、リクエストしてコントローラの呼び出し、viewヘルパーの呼び出し』を追加
(1) help
helpはコマンドの一覧と英語での説明を表示してくれるコマンドです。
何をおいてもこれを覚えておけば、コマンドに困ることはないと思いますw
(2) cd と ls
cdはオブジェクトへの移動。
lsはオブジェクトのメソッドや、変数の表示。
Pryの基本中の基本のコマンドですが超便利・超重要です!
1
2
3
4
5
6
7
[ 1 ] pry ( Object ): 1 > images = Image . all
ImageSite Load ( 10 . 4 ms ) SELECT `images` . * FROM `images`
[ 2 ] pry ( Object ): 1 > cd images
[ 3 ] pry ( #<Array>):2> ls
Enumerable #methods: all? any? chunk ...
self . methods : __pry__
locals : _ _dir_ _ex_ _file_ _in_ _out_ _pry_
(3) 特別なローカル情報
次はSpecial Locals(特別なローカル情報)を表示するための7つのコマンドです。使いこなせれば結構便利です。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# _ : 1つ前のコマンドの結果
pry( main) > 5 + 5
= > 10
pry( main) > _ + 10
= > 20
# _ex_ : 前に発生した例外情報
pry( main) > 4 + "j"
TypeError: String can't be coerced into Fixnum
from (pry):2:in `+'
pry( main) > _ex_
= > #<TypeError: String can't be coerced into Fixnum>
# _in_ : 入力値
# _out_ : 出力値
[ 25] pry( main) > 1 + 1
= > 2
[ 26] pry( main) > 2 + 3
= > 5
pry( main) > _out_[ 25] + _out_[ 26]
= > 7
# _dir_ : ディレクトリパス
# _file_ : ファイルパス
pry( main) > show-method pry
From: /Users/john/ruby/projects/pry/lib/pry/core_extensions.rb @
....
pry( main) > _file_
= > "/Users/john/ruby/projects/pry/lib/pry/core_extensions.rb"
(4) find-method
find-methodはメソッドを検索してくれます。
(5) show-models
MySQLのテーブル定義を参照できるのが、show-modelsです。
(6) show-routes
コマンドは次のとおりです。
1
2
show-routes #<= ルーティングの一覧を表示
show-routes --grep new #<= newに絞って表示
rake routesより高速です!
(7) show-middlewares
参照しているmiddlewareの一覧を表示してくれるのが、show-middlewaresです。
(8) reload!
reload!はRails環境全体をリロードしてくれます。
(9) 自作のPryコマンドの追加
Pryコマンドの自作もできます。
プロジェクト専用のPryコマンドを作成する場合は、.pryrcをプロジェクト直下に作成。
共通して使うPryコマンドを作成する場合は、~/.pryrcを作成。
作成したファイルに下記のように追加。
1
2
3
4
5
6
command_set = Pry :: CommandSet . new do
command "コマンドの名前" , "コマンドの説明" do | 引数 |
# ここに処理を書く
end
end
Pry . config . commands . import my_command_set
ちなみに、このGist には次のような便利なコマンドがありました。
コマンド
意味・機能
copy
引数をクリップボードにコピー
clear
現在のPryの表示をクリア
sql
引数のSQLを実行
caller_method
呼び出し元のメソッドを表示
この4つのコマンドを使いたい場合は、下を.pryrcに追加して下さい。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
%w[ rubygems pp logger] .each do |gem|
begin
require gem
rescue LoadError
end
end
Pry.config.editor = "mate -w"
# Default Command Set, add custom methods here:
default_command_set = Pry::CommandSet.new do
command "copy" , "Copy argument to the clip-board" do |str|
IO.popen( 'pbcopy' , 'w' ) { |f| f << str.to_s }
end
command "clear" do
syst em 'clear'
if ENV[ 'RAILS_ENV' ]
output.puts "Rails Environment: " + ENV[ 'RAILS_ENV' ]
end
end
command "sql" , "Send sql over AR." do |query|
if ENV[ 'RAILS_ENV' ] || defined?( Rails)
pp ActiveRecord::Base.connection.select_all( query)
else
pp "Pry did not require the environment, try `pconsole`"
end
end
command "caller_method" do |depth|
depth = depth.to_i || 1
if /^( .+?) :( \d +)( ?::in ` ( .*) ' ) ?/ = ~ caller ( depth+1) .first
file = Regexp.last_match[ 1]
line = Regexp.last_match[ 2] .to_i
method = Regexp.last_match[ 3]
output.puts [ file, line, method]
end
end
end
Pry.config.commands.import default_command_set
Pry.config.should_load_plugins = false
(10) urlヘルパーが生成するpath/urlの確認
1
2
3
4
5
app . product_index_path
#=> "/product"
app . admin_product_index_url
#=> "http://www.example.com/product"
(11) リクエストしてコントローラの呼び出し
1
2
3
4
5
6
app . get "http://0.0.0.0:300/"
. . . .
#=> 200
app . response . body
#=> レスポンスのhtmlが表示
(12) viewヘルパーの呼び出し
1
2
3
4
5
helper . number_to_currency ( 7777777 )
#=> "7,777,777円"
helper . image_tag "https://www.google.co.jp/images/srpr/logo4w.png"
#=> "<img alt=\"Logo4w\" src=\"https://www.google.co.jp/images/srpr/logo4w.png\" />"
Pry公式Wiki
Pryは公式のWikiが充実しているので、もし良かったらぜひ見てみて下さい!
pry/pry Wiki
こちらもどうぞ:Pryをさらに強力にしてくれる拡張機能
Pryの拡張機能の紹介は、拙作の「Pryの秘めた力を最大限引き出す4つの拡張機能 」 をご参照下さい!
Special Thanks
Happy Elements Labs: Rubyist必携 pry-railsを使いこなせば幸せになれる
rails console の tips #Ruby #Rails - Qiita