LoginSignup
10

More than 5 years have passed since last update.

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

Posted at

Ruby の 定番対話ツール pry 徹底攻略 | :baby_chick::baby_chick::bird::baby_chick::baby_chick: Exception handling

:musical_score: 概要

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

:baby_chick::baby_chick::bird::baby_chick::baby_chick: Exception handling

pry はあなたのコードで起こったほとんどの例外をキャッシュしている。
そのため、何が間違っていたか原因を突き止め、問題をなおす助力となる。

:station: The _ex_ local variable

pry は一番最後に発生した例外を _ex_ というローカル変数に保持している。

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

:deciduous_tree: Using the wtf? command

_ex_ の代替として wtf? を利用することもできます。
wtf という言葉についてググったのですが、これですかね?
"what the :fu:?" : "何だ一体?"

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

:scream_cat: Using the cat --ex command

cat --ex でエラーが発生した行をシンタックスハイライト付きで表示します。

シンタックスハイライトの確認

color.png

スタック階層の指定

引数に数値 N を指定することで、例外発生時の任意の階層を確認できます

  • 対象コード
def hoge1
  hoge2
end

def hoge2
  hoge3
end

def hoge3
  raise "error"
end

hoge1
  • 実行確認
$ pry -I ./hoge.rb
[3] pry(main)> edit hoge.rb
RuntimeError: error
from /path/to/current/hoge.rb:10:in `hoge3'
[3] pry(main)> hoge1
RuntimeError: error
from /path/to/current/hoge.rb:10:in `hoge3'

# N 指定なしで呼び出し
[4] pry(main)> cat --ex

Exception: RuntimeError: error
--
From: /path/to/current/hoge.rb @ line 10 @ level: 0 of backtrace (of 30).

     5: def hoge2
     6:   hoge3
     7: end
     8:
     9: def hoge3
 => 10:   raise "error"
    11: end
# N=1 で呼び出し
[5] pry(main)> cat --ex 1

Exception: RuntimeError: error
--
From: /path/to/current/hoge.rb @ line 6 @ level: 1 of backtrace (of 30).

     1: def hoge1
     2:   hoge2
     3: end
     4:
     5: def hoge2
 =>  6:   hoge3
     7: end
# N=2 で呼び出し
[6] pry(main)> cat --ex 2

Exception: RuntimeError: error
--
From: /path/to/current/hoge.rb @ line 2 @ level: 2 of backtrace (of 30).

    1: def hoge1
 => 2:   hoge2
    3: end
    4:
    5: def hoge2
    6:   hoge3
    7: end
# N=3 で呼び出し
[7] pry(main)> cat --ex 3

Exception: RuntimeError: error
--
From: (pry) @ line 2 @ level: 3 of backtrace (of 30).

    1: hoge1
 => 2: hoge1
[8] pry(main)>

:pouting_cat: Configuring cat --ex

cat --ex は、デフォルトで 5 行の出力を行います。
出力行数を変更したい場合は、 Pry.config.default_window_size を設定します。

Pry.config.default_window_size に関する詳細については
Ruby の 定番対話ツール pry 徹底攻略 | Customization and configuration を参照

Using the edit --ex command

edit --ex で、最後にエラーが発生した処理の該当ファイル・該当行から編集を始めることができます。
cat --ex N と同様に edit --ex N で任意の階層を指定することもできます。
これにより、エラー原因の素早い修正が可能になります。

:sound::seat::wrench::red_car: Customising the exception handler

pry が例外を補足した時、デフォルトではあなたがのぞむ十分な情報ではないと感じるかもしれません。
Pry.config.exception_handler に独自の例外情報の出力設定を行うことで、自分好みの内容にすることができます。

Pry.config.exception_handler に関する詳細については
Ruby の 定番対話ツール pry 徹底攻略 | Customization and configuration を参照

Configuring which exceptions are caught

pry は whitelist によって、あえて補足しない例外を管理することができます。
デフォルトではこれによって、 SystemExit などが無視されています。
whitelist は Pry.config.exception_whitelist で設定可能です。

Pry.config.exception_whitelist に関する詳細については
Ruby の 定番対話ツール pry 徹底攻略 | Customization and configuration を参照

:police_car: The pry-rescue plugin

よりパワフルな例外ハンドリング機能は、 pry-rescue にある!
と、公式に書いてあったので確認したら、 pry-rescue の Travis CI のテストが Fail していた。
不安を覚えつつ動作確認してみましたが、公式ページのサンプル通りに動かなかったので確認を切り上げます。

: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
10