binding.pryだけじゃなかったpry

## はじめに
デバッグについて調べていた際にpryのことをついて勉強。
binding.pryくらいしか使ってこなかったデバッグツールについて簡単にまとめておきます。


## pryって？
Rubyには標準ではirb(Interactive Ruby)が装備されていますが、
それをより便利にしたものになります。
初学者の方もirbを起動させてrubyのコードを打ったことがあるのではないでしょうか？

Railsではデバッグ（バグなどを発見すること）をする際に使われます。

pryを使うには`Gemfile`にgemを追加する必要があります。

有名なもので**pry-rails**と**pry-byebug**があります。
今回は**pry-rails**で説明します。


```ruby:Gemfile
gem 'pry-rails'
```
`Gemfile`に追加して

```console
% bundle install
```

bundle installしましょう。
これで使えるようになります。


## pryを使うことのメリット
1. **便利なコマンドが標準装備されている**
1. **shellコマンドが使用可能**

## 便利なコマンドが標準装備


よく使われるのは`binding.pry`だと思います。

### binding.pryとは
投稿フォームに入力した情報がデータベースに保存されないというエラーが発生したとします。
そういった場合、原因がどのコードにあるのかをbinding.pryという文字列をコントローラーなどのコード内に記述するとそこがブレイクポイントとなりrailsの処理を止めることができます。
その場所でpryが記述して変数の中身やパラメーターなどの確認をすることができます。

### 便利なコマンド一例

pryでは他に便利な機能が用意されていて
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.
:...skipping...
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
  !                  Clear the input buffer.
  amend-line         Amend a line of input in multi-line mode.
  edit               Invoke the default editor on a file.
  hist               Show and replay readline history.
  play               Playback a string variable, method, line, or file as input.
  show-input         Show the contents of the input buffer for the current multi-line expression.

Introspection
  ri                 View ri documentation.
  show-doc           Show the documentation for a method or class.
  show-source        Show the source for a method or class.
:...skipping...
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
  !                  Clear the input buffer.
  amend-line         Amend a line of input in multi-line mode.
  edit               Invoke the default editor on a file.
  hist               Show and replay readline history.
  play               Playback a string variable, method, line, or file as input.
  show-input         Show the contents of the input buffer for the current multi-line expression.

Introspection
  ri                 View ri documentation.
  show-doc           Show the documentation for a method or class.
  show-source        Show the source for a method or class.
  stat               View method information and set _file_ and _dir_ locals.

Commands
  import-set         Import a pry command set.

:...skipping...
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
  !                  Clear the input buffer.
  amend-line         Amend a line of input in multi-line mode.
  edit               Invoke the default editor on a file.
  hist               Show and replay readline history.
  play               Playback a string variable, method, line, or file as input.
  show-input         Show the contents of the input buffer for the current multi-line expression.

Introspection
  ri                 View ri documentation.
  show-doc           Show the documentation for a method or class.
  show-source        Show the source for a method or class.
  stat               View method information and set _file_ and _dir_ locals.

Commands
  import-set         Import a pry command set.

Aliases
  !!!                Alias for `exit-program`
  !!@                Alias for `exit-all`
  $                  Alias for `show-source`
  /whereami[!?]+/    Alias for `whereami`
:...skipping...
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
  !                  Clear the input buffer.
  amend-line         Amend a line of input in multi-line mode.
  edit               Invoke the default editor on a file.
  hist               Show and replay readline history.
  play               Playback a string variable, method, line, or file as input.
  show-input         Show the contents of the input buffer for the current multi-line expression.

Introspection
  ri                 View ri documentation.
  show-doc           Show the documentation for a method or class.
  show-source        Show the source for a method or class.
  stat               View method information and set _file_ and _dir_ locals.

Commands
  import-set         Import a pry command set.

Aliases
  !!!                Alias for `exit-program`
  !!@                Alias for `exit-all`
  $                  Alias for `show-source`
  /whereami[!?]+/    Alias for `whereami`
  ?                  Alias for `show-source -d`
  @                  Alias for `whereami`
  file-mode          Alias for `shell-mode`
  find-routes        Alias for `find-route`
:...skipping...
Help
  help               Show a list of commands or information about a specific command.

Context
  cd                 Move into a new context (object or scope).
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
  !                  Clear the input buffer.
  amend-line         Amend a line of input in multi-line mode.
  edit               Invoke the default editor on a file.
  hist               Show and replay readline history.
  play               Playback a string variable, method, line, or file as input.
  show-input         Show the contents of the input buffer for the current multi-line expression.

Introspection
  ri                 View ri documentation.
  show-doc           Show the documentation for a method or class.
  show-source        Show the source for a method or class.
  stat               View method information and set _file_ and _dir_ locals.

Commands
  import-set         Import a pry command set.

Aliases
  !!!                Alias for `exit-program`
  !!@                Alias for `exit-all`
  $                  Alias for `show-source`
  /whereami[!?]+/    Alias for `whereami`
  ?                  Alias for `show-source -d`
  @                  Alias for `whereami`
  file-mode          Alias for `shell-mode`
  find-routes        Alias for `find-route`
  history            Alias for `hist`
  quit               Alias for `exit`
  quit-program       Alias for `exit-program`
  reload-method      Alias for `reload-code`
  show-method        Alias for `show-source`

Input and output
  .<shell command>   All text following a '.' is forwarded to the shell.
  cat                Show code from a file, pry's input buffer, or the last exception.
  change-inspector   Change the current inspector proc.
  change-prompt      Change the current prompt.
  clear-screen       Clear the contents of the screen/window pry is running in.
  fix-indent         Correct the indentation for contents of the input buffer
  list-inspectors    List the inspector procs available for use.
  save-file          Export to a file using content from the repl.
  shell-mode         Toggle shell mode. bring in pwd prompt and file completion.

Misc
  pry-version        Show pry version.
  reload-code        Reload the source file that contains the specified code object.
  toggle-color       Toggle syntax highlighting.

Navigating pry
  !pry               Start a pry session on current self.
  disable-pry        Stops all future calls to pry and exits the current session.
  exit               Pop the previous binding.
  exit-all           End the current pry session.
  exit-program       End the current program.
  jump-to            Jump to a binding further up the stack.
  nesting            Show nesting information.
  switch-to          Start a new subsession on a binding in the current stack.

Rails
  find-route         See which urls match a given controller.
  recognize-path     See which route matches a url.
  show-middleware    Show all middleware (that rails knows about).
  show-model         Show the given model.
  show-models        Show all models.
  show-routes        Show all routes in match order.
(END)
```

たくさんあります。

### show-routes
例えば`show-routes`というコマンドはルーティングを確認することができます。
ルーティングの確認だと`rails routes`があるじゃんと思うかと思いますが
`show-routes`はより高速に表示されます。

### show-models
`show-models｀というコマンドはアプリケーションにある全てのモデル、カラム、データタイプを返してくれるコマンドです。


## pryでshellコマンドを使う
pryでshellコマンドをコンソール上で入力するには`.`をつけて実行しないとエラーになります。

[![Image from Gyazo](https://i.gyazo.com/4f54bcf4ae817669e6244d3a2df08865.png)](https://gyazo.com/4f54bcf4ae817669e6244d3a2df08865)

[![Image from Gyazo](https://i.gyazo.com/b3d43f2def995f97d3e74ef14cc1ed17.png)](https://gyazo.com/b3d43f2def995f97d3e74ef14cc1ed17)


## 終わりに
なんとなく使っていたものもいろいろ便利な機能があるのだと思いました。
深掘りって大事ですね。
日々更新していきます。

>参考サイト
[Pikawaka 【Rails】Pryについて徹底解説！](https://pikawaka.com/rails/pry#Pry%E3%81%AE%E4%BD%BF%E3%81%84%E6%96%B9)
[TechTechMedia 【Rails】Rails開発者に絶対おすすめしたいデバッグ手法５選（初心者向け）](https://techtechmedia.com/debug-summary-rail/)