Nik So
Nik So

Reputation: 16821

pry gem how to reload?

I am using the Pry gem in my Rails console, but the pry flavored rails-console seems to have lost the reload! method for reloading models and stuff.

Here's how I start the pry console

c:\rails\app> pry -r ./config/environment

Thank You

Upvotes: 41

Views: 20193

Answers (9)

Swaps
Swaps

Reputation: 1568

A better version of @Rodrigo Dias's answer. If you don't want to use pry-rails gem then just add following into your .pryrc-

if defined?(Rails) && Rails.env
  if defined?(Rails::ConsoleMethods)
    include Rails::ConsoleMethods
  else
    def reload!(print=true)
      puts "Reloading..." if print
      ActionDispatch::Reloader.cleanup!
      ActionDispatch::Reloader.prepare!
      true
    end
  end
end

This code properly identifies environments and doesn't blindly includes Rails::ConsoleMethods.

Source - Github thread comment

Upvotes: 1

guapolo
guapolo

Reputation: 2543

If you are having trouble with Zeus and Pry, try adding to your .pryrc:

if Kernel.const_defined?(:Rails) && Rails.env
  require File.join(Rails.root,"config","environment")
  require 'rails/console/app'
  require 'rails/console/helpers'
  extend Rails::ConsoleMethods
end

Taken from here

Upvotes: 3

Rodrigo Dias
Rodrigo Dias

Reputation: 1340

To use reload! like the rails console command, add this code to your .pryrc

# load Rails Console helpers like reload
require 'rails/console/app'
extend Rails::ConsoleMethods
puts 'Rails Console Helpers loaded'

EDIT== Gem pry-rails already do all of this, much simplier .

Upvotes: 24

horseyguy
horseyguy

Reputation: 29915

You could check out this page on the Pry wiki: https://github.com/pry/pry/wiki/Setting-up-Rails-or-Heroku-to-use-Pry

Also check out the pry-rails plugin: https://github.com/rweng/pry-rails

There's also a lot of other content on that wiki, it's a great resource.

Upvotes: 6

not an ai
not an ai

Reputation: 1823

For anyone coming to this question recently: the answer has changed in Rails 3.2, because they've changed how they implement reload! Where in earlier version the irb commands were added as methods to Object, now they are added to IRB::ExtendCommandBundle to avoid polluting the global namespace.

What I do now is (1) in development.rb

silence_warnings do
  begin
    require 'pry'
    IRB = Pry
    module Pry::RailsCommands ;end
    IRB::ExtendCommandBundle = Pry::RailsCommands
  rescue LoadError
  end
end

and (2) in .pryrc

if Kernel.const_defined?("Rails") then
  require File.join(Rails.root,"config","environment")
  require 'rails/console/app'
  require 'rails/console/helpers'
  Pry::RailsCommands.instance_methods.each do |name| 
    Pry::Commands.command name.to_s do 
      Class.new.extend(Pry::RailsCommands).send(name)
    end
  end
end

Here's the link to the Rails pull request where the change was introduced - https://github.com/rails/rails/pull/3509

Upvotes: 12

grosser
grosser

Reputation: 15097

alias pryr="pry -r ./config/environment -r rails/console/app -r rails/console/helpers"

Upvotes: 2

Simon Ernst
Simon Ernst

Reputation: 1430

You could tell Pry to load your Rails environment in your .pryrc

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

This will give your reload! back.

Upvotes: 6

lucapette
lucapette

Reputation: 20724

I've recently written a post about pry and rails. You can find it here http://lucapette.com/pry/pry-everywhere/. By the way, as dave already said, you would like to use pry with:

pry -r ./config/environment

I recommend you to try what I wrote in the article, it works really fine.

Upvotes: 2

Dave Newton
Dave Newton

Reputation: 160311

Do you mean ./config/environment?

In any case, I think that's different than actually launching a rails console, which is where reload! comes from. I redefine IRB = Pry in my env-specific config file, which ensures a full console, and it all works like a charm.

Upvotes: 1

Related Questions