LoginSignup
8
9

More than 5 years have passed since last update.

Ruby の 定番対話ツール pry 徹底攻略 | Editor integration #pry #ruby

Posted at

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

:musical_score: 概要

Ruby の 定番対話ツール pry 徹底攻略
Editor integration について

Editor integration

Pry は shell-like なプロンプトを持ち、メソッド名のタブ補完などができる。
しかし、エディタを使いたいこともあるだろう。

:raised_hand: Using the edit command

edit command でデフォルトのエディタを呼び出します。

edit <filename.rb>

edit command は、編集後にあなたが作成したファイルを load します。

$ pry
[1] pry(main)> edit hoge.rb
  • エディタが起動。下記の内容で編集してエディタを終了する
class Hoge
  def hoge
    "hoge"
  end
edn

Hoge.new.hoge
  • 自動で load されたことを確認
[2] pry(main)> Hoge.new.hoge
=> "hoge"
[3] pry(main)>
  • その他の option は help command で参照
[1] pry(main)> edit -h
Usage: edit [--no-reload|--reload|--patch] [--line LINE] [--temp|--ex|FILE[:LINE]|OBJECT|--in N]

Open a text editor. When no FILE is given, edits the pry input buffer.
When a method/module/command is given, the code is opened in an editor.
Ensure `Pry.config.editor` or `_pry_.config.editor` is set to your editor of choice.

edit sample.rb                edit -p MyClass#my_method
edit sample.rb --line 105     edit MyClass
edit MyClass#my_method        edit --ex
edit --method                 edit --ex -p

https://github.com/pry/pry/wiki/Editor-integration#wiki-Edit_command

    -e, --ex             Open the file that raised the most recent exception (_ex_.file)
    -i, --in             Open a temporary file containing the Nth input expression. N may be a range (default: -1..-1)
    -t, --temp           Open an empty temporary file
    -l, --line           Jump to this line in the opened file
    -n, --no-reload      Don't automatically reload the edited file
    -c, --current        Open the current __FILE__ and at __LINE__ (as returned by `whereami`)
    -r, --reload         Reload the edited code immediately (default for ruby files)
    -p, --patch          Instead of editing the object's file, try to edit in a tempfile and apply as a monkey patch
    -m, --method         Explicitly edit the _current_ method (when inside a method context).
    -h, --help           Show this message.

Setting the default editor

Pry.config.editor に任意のエディタを設定することで、起動するエディタを変更できます。
エディタの設定を nano から vi に変更します。

pry を起動し、現在のエディタを確認

$ pry
[1] pry(main)> Pry.config.editor
=> "nano"
[2] pry(main)> edit nano.rb

nano で編集

class Nano
  def nano
    "nano"
  end
end

編集後、 pry に戻り、ファイルが load されている

[3] pry(main)> Nano.new
=> #<Nano:0x00000603626890>
[4] pry(main)> Nano.new.nano
=> "nano"
[5] pry(main)> exit
$

~/.pryrc でエディタを vi に設定

$ echo 'Pry.config.editor = "vi"' > ~/.pryrc
$ cat ~/.pryrc
Pry.config.editor = "vi"

pry を起動し、現在のエディタを確認

$ pry
[1] pry(main)> Pry.config.editor
=> "vi"
[2] pry(main)> edit vi.rb
[3] pry(main)> Vi.new.vi
=> "vi"
[4] pry(main)> exit
$

vi で編集

class Vi
  def vi
    "vi"
  end
end

編集後、 pry に戻り、ファイルが load されている

[7] pry(main)> Vi.new
=> #<Vi:0x000006034effa8>
[8] pry(main)> Vi.new.vi
=> "vi"

:shell: Invoking an editor through the shell

Shell Integrationを利用して、エディタを呼び出すこともできます。

[9] pry(main)> .vi vim.rb
  • vim で編集する
class Vim
  def vim
    "vim"
  end
end
  • vim 終了後に、作成したファイルからクラスを呼び出す
[15] pry(main)> require './vim.rb'
=> true
[16] pry(main)> Vim.new
=> #<Vim:0x00000603dfb110>
[17] pry(main)> Vim.new.vim
=> "vim"

:man::woman: 親記事

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

:books: 外部資料

8
9
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
8
9