LoginSignup
3
3

More than 5 years have passed since last update.

Ruby の 定番対話ツール pry 徹底攻略 | Creating custom commands #pry #ruby

Posted at

Ruby の 定番対話ツール pry 徹底攻略 | Creating custom commands

:musical_score: 概要

Ruby の 定番対話ツール pry 徹底攻略
カスタムコマンドの作成について

Creating custom commands

Pry は一般的なユースケースに対応した多数のビルトインコマンドとともに配布されています。
残念ながら誰にでも便利とは限らない機能については、含めようとは思いません。
そのため、Pry 提供側としては簡単にカスタムコマンドを定義できるようにしました。

:musical_note: Block commands

Block command はシンプルなコマンドを素早く作る方法です。
Class command と異なり、オプションをとることができません。

Block command の定義には Pry::CommandSet#block_command の構文を利用します。

サンプル

[2] pry(main)> Pry::Commands.block_command "triangle", "calculate triangle area" do |width, height|
[2] pry(main)*   output.puts (width.to_i * height.to_i) / 2
[2] pry(main)* end
=> #<class(Pry::Command "triangle")>
[3] pry(main)> triangle 2 2
2
[4] pry(main)> triangle 2 4
4
[5] pry(main)> triangle 2 17
17
[6] pry(main)> triangle 10 17
85

:closed_book: ヘルプ

自動的にヘルプにも追加されます。

[9] pry(main)> help triangle
triangle           calculate triangle area

Block command options

Pry::CommandSet#block_command の三番目の引数は、 command option です。
ここでは例として interpolate オプションを利用して、引数の文字列を展開可能にします。

[51] pry(main)> Pry::Commands.block_command "interpolate_message", "show interpolate message", :interpolate => true do |message|
[51] pry(main)*   output.puts message
[51] pry(main)* end
=> #<class(Pry::Command "interpolate_message")>
[54] pry(main)> interpolate_message "prefix#{Time.now}sufix"
prefix2015-02-10 16:28:11 +0900sufix

:musical_note: Class commands

Class commands は Block command に良く似ていますが少し冗長です。

[67] pry(main)> Pry::Commands.create_command "triangle" do
[67] pry(main)*   description "calculate triangle area"
[67] pry(main)*
[67] pry(main)*   def process(width, height)
[67] pry(main)*     output.puts (width.to_i * height.to_i) / 2
[67] pry(main)*   end
[67] pry(main)* end
=> #<class(Pry::Command "triangle")>
[68] pry(main)> triangle 2 2
2
[69] pry(main)> triangle 2 4
4
[70] pry(main)> triangle 2 17
17
[71] pry(main)> triangle 10 17
85

:closed_book: ヘルプ

help コマンドや、 --help オプションによってヘルプを利用可能です。

[74] pry(main)> help triangle
calculate triangle area
    -h, --help      Show this message.
[75] pry(main)> triangle --help
calculate triangle area
    -h, --help      Show this message.

:musical_note: Regexp commands

Block Command, Class Command はどちらも文字列だけではなく正規表現を利用することができます。
面白い定義のコマンドを作成する際に役立ちます。

[91] pry(main)> Pry::Commands.block_command /triangle_(\w+)_(\h+)/, "calculate triangle area" do
[91] pry(main)*   output.puts (captures[0].to_i * captures[1].to_i) / 2
[91] pry(main)* end
=> #<class(Pry::Command /triangle_(\w+)_(\h+)/)>
[92] pry(main)> triangle_2_2
2
[93] pry(main)> triangle_2_4
4
[94] pry(main)> triangle_2_17
17
[95] pry(main)> triangle_10_17
85

:musical_note: Command options

公式ページ参照
Command options | Custom commands

:musical_note: Context methods

すべてのコマンドはいくつかの特別なメソッドにアクセス可能です。

公式ページ参照
Context methods | Custom commands

:musical_note: Conventions

pry のコマンド作成時に必要な慣習について

Ensure all output goes to the output object

出力は puts に直接行わず output に対して行う

Provide non-color variation

カラー付き出力をする場合は、カラーなし出力も提供する。
両者は Pry.config.color によって切り替える。

Use hyphens not underscores for long command names

長いコマンド名にはアンダースコアではなくハイフンを使う。
コマンドをメソッドと区別するためのものです。

Provide a listing for Regexp commands

正規表現を利用したコマンドは、helpshow-source のために listing オプションを設定すること。

[133] pry(main)> Pry::Commands.block_command /@(\w+)\+(\h+)/, "calculate addition", listing: 'addition' do
[133] pry(main)*   output.puts captures[0].to_i + captures[1].to_i
[133] pry(main)* end
=> #<class(Pry::Command /@(\w+)\+(\h+)/)>
[134] pry(main)> help addition
addition           calculate addition
[135] pry(main)> @3+7
10
[137] pry(main)> show-source addition

From: (pry)
Number of lines: 3

Pry::Commands.block_command /(\w+)\+(\h+)/, "calculate addition", listing: 'addition' do
  output.puts captures[0].to_i + captures[1].to_i
end

:man::woman: 親記事

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

:books: 外部資料

3
3
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
3
3