This post is Private. Only a writer or those who know its URL can access this post.
More than 1 year has passed since last update.

よくあるエラー

  • Syntax Error
  • NoMethodError
  • NameError

Syntax Error


NoMethodError, NameError


プリントデバッグ

p "出力したいデバッグ情報"

logger.debug "出力したいデバッグ情報"

ブレイクポイント

通常(ruby2.4以降)

binding.irb

pryを使用

Gemfile
gem 'pry-rails'
binding.pry

exitとexit!

exit だと次の binding.pry で止まりますが、 exit! だと一発でプログラムから出られます。


ブラウザでデバッグ

Better Errors

group :development do
  gem "better_errors"
  gem "binding_of_caller"
end

その他(より見やすく)

Awesome Print

Array, Hashの出力を整形してくれる。

gem 'awesome_print'
~/.pryrc
# awesome_print
begin
  require "awesome_print"
  Pry.config.print = proc { |output, value| output.puts value.ai }
rescue LoadError
  puts "no awesome_print :("
end

Hirb

DBの出力を整形してくれる。

group :development, :test do
  gem 'hirb'         # モデルの出力結果を表形式で表示するGem
  gem 'hirb-unicode' # 日本語などマルチバイト文字の出力時の出力結果のずれに対応
end
~/.pryrc
# hirb
begin
  require "hirb"
rescue LoadError
  puts "no hirb :("
end

if defined? Hirb
  # Slightly dirty hack to fully support in-session Hirb.disable/enable toggling
  Hirb::View.instance_eval do
    def enable_output_method
      @output_method = true
      @old_print = Pry.config.print
      Pry.config.print = proc do |*args|
        Hirb::View.view_or_page_output(args[1]) || @old_print.call(*args)
      end
    end

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

  Hirb.enable
end

お助けツール

Rails Panel

group :development do
  gem 'meta_request'
end

bullet

group :development do
  gem 'bullet'
end

breakman

group :development do
  gem 'breakman'
end

rubocop

group :development do
  gem 'rubocop'
end

rubocop

group :development do
  gem 'rubocop'
end

rails_best_practices

group :development do
  gem "rails_best_practices"
end

Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account log in.