pry gem how to reload?

吃可爱长大的小学妹 提交于 2019-11-28 21:06:20
horseyguy

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.

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 .

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

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.

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.

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

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

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!