LoginSignup
5

More than 5 years have passed since last update.

Ruby の 定番対話ツール pry 徹底攻略 | The command system #pry #ruby

Posted at

Ruby の 定番対話ツール pry 徹底攻略 | The command system

:musical_score: 概要

Ruby の 定番対話ツール pry 徹底攻略
コマンドラインシステムについて

The command system

コマンドシステムは、 pry が持つ他の REPL とは異なる面白い特徴です。

:coffee::flushed: Invoking commands

通常の shell コマンドなどと同様に、コマンド名と必要なオプションなどを渡して呼び出します。

$ pry
[1] pry(main)> ls
self.methods: inspect  to_s
locals: _  __  _dir_  _ex_  _file_  _in_  _out_  _pry_
[2] pry(main)> hoge = "hoge"
=> "hoge"
[3] pry(main)> hige = "hige"e"
=> "hige"
[4] pry(main)> ls -l
hoge = "hoge"
hige = "hige"

:closed_book: The help command

help command でヘルプを表示します。

:bird::bird::baby_chick::baby_chick::chicken::chicken: All Commands Help

引数なしの場合はすべてのコマンドを表示します。

[7] pry(main)> help
Help
  help               Show a list of commands or information about a specific command.

Context
  cd                 Move into a new context (object or scope).
  find-method        Recursively search for a method within a class/module or the current namespace.
  ls                 Show the list of vars and methods in the current scope.
  pry-backtrace      Show the backtrace for the pry session.
  raise-up           Raise an exception out of the current pry instance.
  reset              Reset the repl to a clean state.
  watch              Watch the value of an expression and print a notification whenever it changes.
  whereami           Show code surrounding the current context.
  wtf?               Show the backtrace of the most recent exception.

Editing
  /^\s*!\s*$/        Clear the input buffer.
  amend-line         Amend a line of input in multi-line mode.

# 略
[8] pry(main)>

:chicken::chicken: Category Commands Help

カテゴリ名で検索するとカテゴリ単位でヘルプを表示します。

[13] pry(main)> help gems
Gems
  gem-cd             Change working directory to specified gem's directory.
  gem-install        Install a gem and refresh the gem cache.
  gem-list           List and search installed gems.
  gem-open           Opens the working directory of the gem in your editor.

:bird: Single Command Help

コマンド名で検索するとコマンド単位でヘルプを表示します。

[16] pry(main)> help gem-list
Usage: gem-list [REGEX]

List all installed gems, when a regex is provided, limit the output to those
that match the regex.

    -h, --help      Show this message.

:koko: The command prefix

コマンドの評価は通常の Ruby のコードよりも優先されます。
大抵は問題になりませんが、Ruby のコードを優先したい場合はコマンドのプリフィックスに
セミコロン ; を付与します。

$ pry
[1] pry(main)> class Hoge
[1] pry(main)*   def ls
[1] pry(main)*     "ls"
[1] pry(main)*   end
[1] pry(main)* end
=> :ls
[2] pry(main)> hoge = Hoge.new
=> #<Hoge:0x00000600e76ae8>
[3] pry(main)> cd hoge
[4] pry(#<Hoge>):1> ls
Hoge#methods: ls
self.methods: __pry__
locals: _  __  _dir_  _ex_  _file_  _in_  _out_  _pry_
[5] pry(#<Hoge>):1> ;ls
=> "ls"

公式サイトの説明だと、本当はスペースでも同様のことができるようなのですが、
私の環境では動作しませんでした。

Command interpolation

#{} シンタックスを利用することで、システムコマンドに Ruby の実行結果を渡すことができます。
詳細は下記記事にて。

Ruby の 定番対話ツール pry 徹底攻略 | :star::station: Special Locals

Custom commands (overview)

Pry 自身のコマンドを利用すること以外に、実行時や .pryrc で定義したコマンドを利用することができます。
コマンドの定義には Pry.commands.block_command を利用します。
詳細は、custom commands のページにて記述します。

Command Sets (overview)

各 Command は Command Set にまとめられます。
そして、 Pry::Commands.import によって利用することができます。
詳細は、custom commands のページにて記述します。

:man::woman: 親記事

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

:books: 外部資料

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
5