1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【初心者】Rubyでbinding.pryを使う方法【絶対使うべき】

Last updated at Posted at 2020-12-08

概要

勉強会で、Rubyで「binding.pry」を使えるようにする方法を学びましたので、まとめました。

設定方法

①Gemfileを作成

ターミナル
 touch Gemfile

Railsではなく、rubyのフォルダにGemfileを作るという考え方がなかったので、驚きました。
Github:https://github.com/pry/pry

②Gemfileを記述

githubを参考に、コピペします

Gemfile
gem 'pry', '~> 0.13.1'

③インストール

ターミナル
bundle install

④設定

main.rb
# pryを読み込みたいファイルに記述する
require'pry'

⑤使い方

main.rb
# 止めたいところに記述
binding.pry

上記を入力してある状態でコードを実行すると
「binding.pryを記述したところ」でとまるので、以下のようなことを試してください。

  • 変数に何が格納されているか?
  • 期待している値は入っているか?
  • binding.pryの部分で止まるのか?そうでない部分でエラーが出るか?

以上になります。

まとめ

binding.pryを使えるようになって、putsやpに出力させる必要がなくなり、開発効率が上がりました。
難しそうと思って使っていなかった過去の自分を叱りたいです。
使ったことがない方は使ってみてください。

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?