Last Updated: January 22, 2021
·
4.667K
· logical42

Using Pry with Rails 4 (with console helpers)

In your "config/application.rb" file:

module MyRailsApp
  class Application < Rails::Application
    #.. default configuration stuff here
    console do
      require "pry"
      config.console = Pry
      unless defined? Pry::ExtendCommandBundle
        Pry::ExtendCommandBundle = Module.new
      end
      require "rails/console/app"
      require "rails/console/helpers"
      TOPLEVEL_BINDING.eval('self').extend ::Rails::ConsoleMethods
    end
  end
end

You'll need to declare pry as a dependency in your Gemfile, of course.

4 Responses
Add your response

Why not simply add pry-rails gem? IMHO a little bit easier and cleaner.

over 1 year ago ·

It feels a bit like overkill to add another rails engine on top of your existing gems for what seems to amount to 10 lines of code. But it's really just a matter of preference, I suppose..

over 1 year ago ·

Thanks. Worked well with rails 4.

over 1 year ago ·

I just needed Rails::ConsoleMethods so I just used this:

console do
   TOPLEVEL_BINDING.eval('self').extend ::Rails::ConsoleMethods
end

Cheers!

over 1 year ago ·