LoginSignup
997
991

More than 5 years have passed since last update.

Rails4 今のところ最強なデバッグツール達

Last updated at Posted at 2013-10-23

Gemfile の 「group :test, :development do」ブロックに書いている 個人的に最強な設定 を晒したいと思います。(2013/10/24 現在)

作業環境

  • Ruby 2.0.0p247
  • Rails 4.0.0

Gemfileに設定している内容

※ コメントに随時変更したものを追記しますー

Gemfile
group :test, :development do
  gem 'pry-rails'
  gem 'pry-doc'
  gem 'pry-stack_explorer'

  if RUBY_VERSION >= '2.0.0'
    gem 'pry-byebug'
  else
    # 以下はRuby1.9の時のみ使う(pry-byebugの代わりに)
    # debuggerは1.9以下でしか動作しない, remote は byebug で使えないようになった
    gem 'pry-debugger'
    gem 'pry-remote'
  end

  gem 'better_errors'
  gem 'binding_of_caller'

  gem 'hirb'
  gem 'hirb-unicode'

  gem 'tapp'
  gem 'awesome_print'
  gem 'quiet_assets'
  gem 'annotate', git: 'git://github.com/ctran/annotate_models.git'
  gem 'timecop'
  gem 'rspec-rails'
  gem 'guard-rspec'
  gem 'factory_girl_rails'
  gem 'database_rewinder'
end

1.pry-rails について

rails consoleをirbではなくpryにしてくれる
https://github.com/rweng/pry-rails

2.pry-doc について

pry上でクラスやメソッドなどのドキュメントや定義元のソースコードを確認できる

pry-doc
■ドキュメントを見る
show-doc String.nil?
OR
? String.nil?

■ソースを見る
show-source String.nil?
OR
$ String.nil?

3 デバッガーの各種Gem

3-Ⅰ. pry-byebug(ruby2.0以上専用) について

ブレークポイントを仕込みたいソースコード上にbinding.pry という文字列を仕込むことで、rails serverで起動したコンソール上でブレークポイントを通るときにpryによるデバッガが立ち上がる。

注) pry-byebug は Ruby2.0 以上 でしか使えない
Ruby 2.0 向けのデバッガー byebugについて
pryでのデバッグ中にnextがstepの様な挙動をしてしまう問題の解決法
About-byebug-for-debug-with-pry

3-Ⅱ. pry-stack_explorer について

pry-stack_explorerはデバッガ起動時にshow-stackと入力することでその時のスタックの情報が見られる。

■pry-stack_explorerで使えるコマンド

コマンド 機能
show-stack スタック表示
up 親のフレームに移動
down 参照されているフレームに移動
frame 詳細フレームに移動

3-Ⅲ. pry-debugger(ruby1.9以下専用) について

pry-byebugと使う用途は同じ。

注) pry-debugger は Ruby1.9 以下 でしか使えない。
https://github.com/nixme/pry-debugger
https://github.com/pry/pry/wiki/Available-plugins#pry-debugger

3-Ⅳ. pry-remote について

アプリサーバをバックグランドで動かしている時 (Pow!! または Unicornをデーモンモードで動かしている時など) にpry-remoteというコマンドでデバッグコンソールを立ち上げることができる。
https://github.com/pry/pry/wiki/Available-plugins#wiki-pry-remote

アプリサーバをフォアグランドで動かしている時 (rails server で立ち上げている時など) はpry-byebugpry-debugger のみ入れておけば起動したコンソールから確認できるのでpry-remoteは必要ない。

注)pry-byebugではpry-remoteをサポートしていないので使えない
Dropped pry-remote support というリリースログ

4.better_errors, binding_of_caller について

エラー時にブラウザに表示するエラー画面をいい感じにしてくれる。binding_of_callerを入れることで、エラー画面でREPLを操作できるようになる。
Railsならbetter_errorsを入れるとデバッグがはかどるなぁ

5.hirb, hirb-unicode について

ActiveRecordのデータをテーブル形式で表示してくれる。
http://rochefort.hatenablog.com/entry/20120104/p1
https://github.com/pry/pry/wiki/FAQ#wiki-hirb
Pryの秘めた力を最大限引き出す4つの拡張機能[Rails] 「モデルの中身を綺麗に表示」を参照

pryを開いている時に一時的にhirbを無効/有効にする
> Hirb.disable

> Hirb.enable

6.tapp について

任意のオブジェクトやメソッドに.tappとしてメソッドチェインを形成することで、その時のオブジェクトの状態を出力させる。
メソッドチェインの途中のオブジェクトの状態を確認したいときに、処理を止めることなくデバッグプリントさせる。
http://namakesugi.blog42.fc2.com/blog-entry-108.html

7.awesome_print について

pputs などのようにプリントデバッグにとても有用な ap メソッドを定義してくれる。pryコンソールの出力のフォーマットもawesom_printを利用している。(.pryrcで定義)
http://qiita.com/Linda_pp/items/d75d7c3953faa34a1f0e
https://github.com/pry/pry/wiki/FAQ#wiki-awesome_print

8.quiet_assets について

assets関連のファイルへのアクセスログを出力させないようにする
https://github.com/evrone/quiet_assets

9.annotate について

DBの定義情報を読み取り、ModelのクラスファイルにコメントでDBの情報を書き込む
http://yoshifumisato.jeez.jp/wordpress/post/rails/693
https://github.com/ctran/annotate_models

10.timecop について

一時的に日時を任意に、設定する
https://github.com/travisjeffery/timecop

11.guard-rspec について

https://github.com/johnbintz/guard-rails
Guardfileの定義情報をもとに、ファイル更新の都度テストを実行させる。Guardfileは基本的にguard initコマンドで自動生成したもの。cmd: 'spring rspec -f doc' となっているところだけ変更している状態。

そのためこのファイルでguardを動かすためには以下の2つの条件が必要です。

  1. rspecを入れている
  2. springを入れている
Guardfile
# A sample Guardfile
# More info at https://github.com/guard/guard#readme

guard :rspec, cmd: 'spring rspec -f doc' do
  watch(%r{^spec/.+_spec\.rb$})
  watch(%r{^lib/(.+)\.rb$})     { |m| "spec/lib/#{m[1]}_spec.rb" }
  watch('spec/spec_helper.rb')  { "spec" }

  # Rails example
  watch(%r{^app/(.+)\.rb$})                           { |m| "spec/#{m[1]}_spec.rb" }
  watch(%r{^app/(.*)(\.erb|\.haml|\.slim)$})          { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
  watch(%r{^app/controllers/(.+)_(controller)\.rb$})  { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
  watch(%r{^spec/support/(.+)\.rb$})                  { "spec" }
  watch('config/routes.rb')                           { "spec/routing" }
  watch('app/controllers/application_controller.rb')  { "spec/controllers" }

  # Capybara features specs
  watch(%r{^app/views/(.+)/.*\.(erb|haml|slim)$})     { |m| "spec/features/#{m[1]}_spec.rb" }

  # Turnip features and steps
  watch(%r{^spec/acceptance/(.+)\.feature$})
  watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$})   { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
end

12.factory_girl_rails について

データの生成をフィクスチャからファクトリーに変更する。
https://github.com/thoughtbot/factory_girl

13.database_rewinder について

database_rewinder はテスト毎にデータをクリアすることに対応するためのGem
https://github.com/amatsuda/database_rewinder


ぼくの「.pryrc」

参考のページを見つつ取り入れられるところは取り入れて「.pryrc」を作っている。
「.pryrc」にはpryの設定と、関連するライブラリの連携設定などを記述。
【参考】
https://github.com/JuanitoFatas/dotpryrc/
https://github.com/pry/pry/wiki/Pry-rc

.pryrc
## Settings
Pry.config.color = true
Pry.config.editor = "vim"

Pry.config.prompt = proc do |obj, level, _|
  prompt = ""
  prompt << "#{Rails.version}@" if defined?(Rails)
  prompt << "#{RUBY_VERSION}"
  "#{prompt} (#{obj})> "
end


## Alias
Pry.config.commands.alias_command "lM", "ls -M"
# Ever get lost in pryland? try w!
Pry.config.commands.alias_command 'w', 'whereami'
# Clear Screen
Pry.config.commands.alias_command '.clr', '.clear'


## Below others cooperation

# refs: https://github.com/pry/pry/wiki/FAQ#wiki-awesome_print
if defined? AwesomePrint
  begin
    require 'awesome_print'
    Pry.config.print = proc { |output, value| Pry::Helpers::BaseHelpers.stagger_output("=> #{value.ai}", output) }
    # Pry.config.print = proc { |output, value| output.puts value.ai } #ページングなし
  rescue LoadError => err
    puts "no awesome_print :("
    puts err
  end
end

# refs: https://github.com/pry/pry/wiki/FAQ#wiki-hirb
if defined? Hirb
 Hirb::View.instance_eval do
   def enable_output_method
     @output_method = true
     @old_print = Pry.config.print
     Pry.config.print = proc do |output, value|
       Hirb::View.view_or_page_output(value) || @old_print.call(output, value)
     end
   end

   def disable_output_method
     Pry.config.print = @old_print
     @output_method = nil
   end
 end

 Hirb.enable
end

# refs: https://github.com/deivid-rodriguez/pry-byebug#matching-byebug-behaviour
if defined?(PryByebug)
  Pry.commands.alias_command 'c', 'continue'
  Pry.commands.alias_command 's', 'step'
  Pry.commands.alias_command 'n', 'next'
  Pry.commands.alias_command 'f', 'finish'
end

以上。
ところどころ参考サイトへのリンクを貼らせていただきました!ありがとうございます。

997
991
6

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
997
991