11
11

More than 5 years have passed since last update.

Railsプロジェクトでpryを使う

Last updated at Posted at 2012-12-26

最近ようやくpryを使い始めました。Railsでpryを使う際にpry -r ./config/environmentとしていたのですがreload!ができず、不便だったのでその対処をまとめておきます。

※ここでは、Gemfileに追加しない方法をご紹介します。もし、Gemfileに追加するのであれば、こちらを参照してください。Gemfileに追加する方がbinding.pryとか使えるので便利です。

まずは、関連するgemをいれます。

gemを追加
$ gem install pry pry-doc pry-rails

設定ファイルを作成します。

~/.pryrc
# Load plugins (only those I whitelist)
Pry.config.should_load_plugins = false
Pry.plugins["doc"].activate!

# Launch Pry with access to the entire Rails stack.
# If you have Pry in your Gemfile, you can pass: ./script/console --irb=pry instead.
# If you don't, you can load it through the lines below :)
rails = File.join Dir.getwd, 'config', 'environment.rb'

if File.exist?(rails) && ENV['SKIP_RAILS'].nil?
  require rails

  if Rails.version[0..0] == "2"
    require 'console_app'
    require 'console_with_helpers'
  elsif Rails.version[0..0] == "3"
    require 'rails/console/app'
    require 'rails/console/helpers'
  else
    warn "[WARN] cannot load Rails console commands (Not on Rails2 or Rails3?)"
  end
end

# for Rails 3.2+
if defined?(Rails) && Rails.env
  extend Rails::ConsoleMethods
end

これで、Railsプロジェクトのルートでpryを実行します。

$ pry
11
11
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
11
11