LoginSignup
9
7

More than 5 years have passed since last update.

Ruby の 定番対話ツール pry 徹底攻略 | Customization and configuration #pry #ruby

Last updated at Posted at 2015-01-22

Ruby の 定番対話ツール pry 徹底攻略 | :sound::seat::wrench::red_car: Customization and configuration

:musical_score: 概要

Ruby の 定番対話ツール pry 徹底攻略
Customization and configuration について

:sound::seat::wrench::red_car: Customization and configuration

Pry は API で簡単に設定を変更できる。
以降、個別の機能について触れていきます

:baby: / :sound::seat::wrench::red_car: Per-instance customization

Pry.config は pry のすべてのインスタンスに様々な情報を設定するだけではなく、
個別のインスタンスについてオーバーライドすることも可能です。
値を設定することで、デフォルトの挙動を変更できます。

:large_blue_circle::red_circle: Color

Pry.config.color は色の有無を指定するオプションです。
Default は true です。

color.png

Pager

Pry.config.pager はページャの有無を指定するオプションです。
Default は true です。

Pry.config.pager = true

[13] pry(main)> Pry.config.pager
=> true
[14] pry(main)> String.methods
=> [:try_convert,
 :allocate,
# 略
 :class_exec,
 # 途中でページ制御が発生

Pry.config.pager = false

[16] pry(main)> Pry.config.pager = false
=> false
[17] pry(main)> String.methods
=> [:try_convert,
 :allocate,
# 略
 :__send__,
 :__id__]
 # 途中でページ制御が発生しない

Auto indent

Pry.config.auto_indent は自動インデントの有無を指定するオプションです。
Default は true です。

以下、自分でまったくインデントを行わなかった場合の動作です。

Pry.config.auto_indent = true

$ pry
[1] pry(main)>
[2] pry(main)> Pry.config.auto_indent
=> true
[3] pry(main)> def hoge
[3] pry(main)*   "hoge"
[3] pry(main)* end
=> :hoge

Pry.config.auto_indent = false

[4] pry(main)> Pry.config.auto_indent = false
=> false
[5] pry(main)> Pry.config.auto_indent
=> false
[6] pry(main)> def hoge
[6] pry(main)* "hoge"
[6] pry(main)* end
=> :hoge

Correct indent

Pry.config.correct_indentend や 配列を複数行で記述した場合の閉じ括弧など自動インデントの有無を指定するオプションです。
Default は true です。

以下、自分でまったくインデントを行わなかった場合の動作です。

Pry.config.correct_indent = true

$ pry
[1] pry(main)> Pry.config.correct_indent
=> true
[2] pry(main)> class Hoge
[2] pry(main)*   def hoge
[2] pry(main)*     "hoge"
[2] pry(main)*   end
[2] pry(main)* end
=> :hoge
[3] pry(main)> [1,
[3] pry(main)*   2,
[3] pry(main)*   3,
[3] pry(main)* ]
=> [1, 2, 3]

Pry.config.correct_indent = false

[4] pry(main)> Pry.config.correct_indent = false
=> false
[5] pry(main)> Pry.config.correct_indent
=> false
[6] pry(main)> class Hoge
[6] pry(main)*   def hoge
[6] pry(main)*     "hoge"
[6] pry(main)*     end
[6] pry(main)*   end
=> :hoge
[7] pry(main)> [1,
[7] pry(main)*   2,
[7] pry(main)*   3,
[7] pry(main)*   ]
=> [1, 2, 3]

The command prefix

Pry.config.command_prefix は pry の command にプリフィックスを設定するオプションです。
Default は 空文字 です。

Pry.config.command_prefix = true

$ pry
[1] pry(main)> Pry.config.command_prefix
=> ""
[3] pry(main)> stat String#upcase
Method Information:
--
Name: upcase
Alias: None.
Owner: String
Visibility: public
Type: Unbound
Arity: 0
Method Signature: upcase()
Source Location: Not found.

Pry.config.command_prefix = false

[4] pry(main)> Pry.config.command_prefix = '@'
=> "@"
[5] pry(main)> stat String#upcase
NoMethodError: undefined method `stat' for main:Object
from (pry):3:in `__pry__'
[6] pry(main)> @stat String#upcase
Method Information:
--
Name: upcase
Alias: None.
Owner: String
Visibility: public
Type: Unbound
Arity: 0
Method Signature: upcase()
Source Location: Not found.

History

Pry.config.history は履歴に関する設定を行ない、様々なプロパティを持ちます。
また、以下の 3 つのサブプロパティを持ちます。
History に関する詳細については Ruby の 定番対話ツール pry 徹底攻略 | Managing History を参照

Editor

Pry.config.editor はエディタ連携に関する設定を行ないます。
Default は $EDITOR です。
ただし、 $EDITOR が未設定の場合は nano が設定されます。

Editor に関する詳細については Ruby の 定番対話ツール pry 徹底攻略 |Editor integration を参照

:recycle: Plugin loading

Pry.config.should_load_plugins は Plugin の自動読み込みの有無を指定するオプションです。
Default は true です。

前提

動作確認用に pry-toys をインストールしました。
pry-toys は pry でメソッドの動きを試す際に役だつダミーデータを手軽に生成してくれます。

[4] pry(main)> require 'pry-toys'
=> true
[5] pry(main)> String.toy(3)
=> "aaa w rrrrrrrrrr"
[6] pry(main)> Array.toy(3)
=> [1, 2, 3]
[7] pry(main)> Array.toy(3, String)
=> ["ccccccccc", "hhh", "vvvvvv"]
[8] pry(main)> Hash.toy(4)
=> {"a"=>1, "b"=>2, "c"=>3, "d"=>4}

デフォルトで動作確認

$ pry
[1] pry(main)> Pry.config.should_load_plugins
=> true
[2] pry(main)> String.toy(3)
=> "lll sss www"

~/.pryrc に Pry.config.should_load_plugins = false を設定して動作確認

$ echo 'Pry.config.should_load_plugins = false' >> ~/.pryrc
$ pry
[1] pry(main)> Pry.config.should_load_plugins
=> false
[2] pry(main)> String.toy(3)
NoMethodError: undefined method `toy' for String:Class
from (pry):2:in `__pry__'

RC-file loading

Pry.config.should_load_rc.pryrc ファイルの読み込み有無を設定します。
Default は true です。

.pryrc ファイルに関する詳細については Ruby の 定番対話ツール pry 徹底攻略 | Runtime invocation を参照

The _in_ and _out_ cache size

Pry.config.should_load_rc_in__out_ のキャッシュサイズを設定します。
Default は 100 です。

_in__out_ に関する詳細については Ruby の 定番対話ツール pry 徹底攻略 | Normal Input を参照

The prompt

Pry.config.prompt はユーザーの入力待ちプロンプトの表示を設定します。
Pry は 2 種類のプロンプトを受け付けます。

  • pry 起動直後などに表示される main prompt
  • 複数行の入力待ちに表示される wait prompt

この 2 種類を配列に設定します。

デフォルトの動作を確認

$ pry -f
[1] pry(main)>
[2] pry(main)> def hoge
[2] pry(main)*
[2] pry(main)*

~/.pryrc で、プロンプトの設定を変更

Pry.config.prompt = [proc { "irb> " }, proc { "____| " }]

プロンプトの表示を確認

$ pry
irb>
irb> def hoge
____|
____|

:musical_note: The prompt prefix

Pry.config.prompt_name はユーザーの入力待ちプロンプトのプリフィックスを設定します。
Default は 'pry' です。

$ pry
[1] pry(main)> Pry.config.prompt_name
=> "pry"
[2] pry(main)> Pry.config.prompt_name = "(^-^)/~~"
=> "(^-^)/~~"
[3] (^-^)/~~(main)> .echo hoge
hoge
[4] (^-^)/~~(main)>

:moyai: The input object

Pry.config.input は入力オブジェクトを設定します。
Default は Readline です。
オブジェクトは readline メソッドを保持していれば設定可能です。

Pry.config.input に File を設定してみます

設定対象ファイル ( hoge.rb )

hoge = "hoge"
hoge.upcase
stat String#upcase
exit

./.pryrcPry.config.input を設定

$ cat ./.pryrc
Pry.config.input = File.new('hoge.rb')

pry を実行

$ pry
[1] pry(main)> hoge = "hoge"
[2] pry(main)> hoge.upcase
[3] pry(main)> stat String#upcase
Method Information:
--
Name: upcase
Alias: None.
Owner: String
Visibility: public
Type: Unbound
Arity: 0
Method Signature: upcase()
[4] pry(main)> exitt found.

:moyai: The output object

Pry.config.output は出力オブジェクトを設定します。
Default は $stdout です。
オブジェクトは puts メソッドを保持していれば設定可能です。

Pry.config.output に File を設定してみます

./.pryrcPry.config.output を設定

$ cat ./.pryrc
Pry.config.output = File.new('output', 'w')

pry を実行

$ pry
[1] pry(main)> hoge = "hoge"
[2] pry(main)> hoge.upcase
[3] pry(main)> stat String#upcase
[4] pry(main)> exit

output ファイルを確認

$ cat output
=> "hoge"
=> "HOGE"
Method Information:
--
Name: upcase
Alias: None.
Owner: String
Visibility: public
Type: Unbound
Arity: 0
Method Signature: upcase()
Source Location: Not found.

The print object

Pry.config.print は print オブジェクトを設定します。
Default は Pry::DEFAULT_PRINT です。

./.pryrcPry.config.print を設定

$ cat ./.pryrc
Pry.config.print = proc { |output, value| output.puts "=> (^-^)/~~#{value.inspect}~~\\(^_^)" }

pry を実行

$ pry
[1] pry(main)> hoge = "hoge"
=> (^-^)/~~"hoge"~~\(^_^)
[2] pry(main)> hoge.upcase
=> (^-^)/~~"HOGE"~~\(^_^)
[3] pry(main)> stat String#upcase
Method Information:
--
Name: upcase
Alias: None.
Owner: String
Visibility: public
Type: Unbound
Arity: 0
Method Signature: upcase()
Source Location: Not found.

:bicyclist: The exception handler

Pry.config.exception_handler は REPL 上でのユーザーの入力がエラーになった場合の動作を設定します。
Default は Pry::DEFAULT_EXCEPTION_HANDLER です。

./.pryrcPry.config.output を設定

$ cat ./.pryrc
Pry.config.exception_handler = proc do |output, exception, _|
  output.puts "exception_message| #{exception.message}"
  output.print "exception_traces_| "
  output.puts exception.backtrace.first(5).join("\n")
end

pry を実行

$ pry
[1] pry(main)> hoge
exception_message| undefined local variable or method `hoge' for main:Object
exception_traces_| (pry):1:in `__pry__'
/path/to/ruby/gems/2.1.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:355:in `eval'
/path/to/ruby/gems/2.1.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:355:in `evaluate_ruby'
/path/to/ruby/gems/2.1.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:323:in `handle_line'
/path/to/ruby/gems/2.1.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:243:in `block (2 levels) in eval'

The exception whitelist

Pry.config.exception_whitelist は pry が無視する例外リストを設定します。
Default は [SystemExit, SignalException] です。

The exception window size

Pry.config.default_window_size は Window サイズを設定します。
この値は、 cat --ex で表示する行数に利用されます。
Default は 5 です。

$ pry
[1] pry(main)> 1
=> 1
[2] pry(main)> 2
=> 2
[3] pry(main)> 3
=> 3
[4] pry(main)> 4
=> 4
[5] pry(main)> 5
=> 5
[6] pry(main)> 6
=> 6
[7] pry(main)> 7
=> 7
[8] pry(main)> 8
=> 8
[9] pry(main)> 9
=> 9
[10] pry(main)> 10
=> 10
[11] pry(main)> 11
=> 11
[12] pry(main)> 12
=> 12
[13] pry(main)> hoge
NameError: undefined local variable or method `hoge' for main:Object
from (pry):13:in `__pry__'
[14] pry(main)> cat --ex

Exception: NameError: undefined local variable or method `hoge' for main:Object
--
From: (pry) @ line 13 @ level: 0 of backtrace (of 27).

     8: 8
     9: 9
    10: 10
    11: 11
    12: 12
 => 13: hoge
[15] pry(main)> Pry.config.default_window_size
=> 5
[16] pry(main)> Pry.config.default_window_size = 10
=> 10
[18] pry(main)> hoge
NameError: undefined local variable or method `hoge' for main:Object
from (pry):16:in `__pry__'
[19] pry(main)> cat --ex

Exception: NameError: undefined local variable or method `hoge' for main:Object
--
From: (pry) @ line 16 @ level: 0 of backtrace (of 27).

     6: 6
     7: 7
     8: 8
     9: 9
    10: 10
    11: 11
    12: 12
    13: hoge
    14: Pry.config.default_window_size
    15: Pry.config.default_window_size = 10
 => 16: hoge

:moyai: The command set object

Pry.config.commands は pry セッションで提供する Pry::CommandSet を設定します。
Default は Pry::Commands です。

Default の動作を確認

[5] pry(main)> Pry.config.commands.map {|e|e.last.name}.take(3)
=> ["Pry::Command::AmendLine", "Pry::Command::Bang", "Pry::Command::BangPry"]
[6] pry(main)> Pry.config.commands.map {|e|e.last.name}.size
=> 81

./.pryrc で独自コマンドと ls だけ使えるように設定

Pry.config.commands = Pry::CommandSet.new do
    import_from Pry::Commands, "ls"
    command "now" do |name|
        output.puts Time.now
    end
end

pry の動作確認

$ pry
[1] pry(main)> now
2015-01-22 17:53:55 +0900
[2] pry(main)> Pry.config.commands
=> #<Pry::CommandSet:0x00000601a25880
 @commands={"ls"=>Pry::Command::Ls, "now"=>#<class(Pry::Command "now")>},
 @helper_module=#<Module:0x00000601a25790>>
[3] pry(main)> ls
self.methods: inspect  to_s
locals: _  __  _dir_  _ex_  _file_  _in_  _out_  _pry_
[4] pry(main)> now
2015-01-22 17:54:26 +0900

:runner: Runtime customization

実行時のカスタマイズを行う場合は、
_pry_ オブジェクトから設定します。

[8] pry(main)> _pry_.prompt = [proc { "irb> " }, proc { "____| " }]
=> [#<Proc:0x00000603f83b18@(pry):7>, #<Proc:0x00000603f83af0@(pry):7>]
irb>
irb> def hoge
____|
____|

:man::woman: 親記事

Ruby の 定番対話ツール pry 徹底攻略

:books: 外部資料

9
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
7