pry gem如何重装?

时间:2011-09-08 11:28:13

标签: ruby-on-rails pry

我在我的Rails控制台中使用Pry gem,但是pry风格的rails-console似乎已经失去了重载!重新加载模型和东西的方法。

以下是我启动pry控制台的方法

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

谢谢

9 个答案:

答案 0 :(得分:19)

使用重装!与rails console命令一样,将此代码添加到.pryrc

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

EDIT == Gem pry-rails已经完成了所有这些,更加简单。

答案 1 :(得分:12)

对于最近提出这个问题的人:Rails 3.2中的答案已经改变了,因为他们已经改变了他们的实现方式reload!在早期版本中,irb命令被添加为Object的方法,现在将它们添加到IRB::ExtendCommandBundle以避免污染全局命名空间。

我现在所做的是(1)在development.rb

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

和(2)在.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

以下是引入更改的Rails pull请求的链接 - https://github.com/rails/rails/pull/3509

答案 2 :(得分:6)

您可以告诉Pry在.pryrc

中加载您的Rails环境
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

这会让你reload!回来。

答案 3 :(得分:6)

您可以在Pry wiki上查看此页面:https://github.com/pry/pry/wiki/Setting-up-Rails-or-Heroku-to-use-Pry

另请查看pry-rails插件:https://github.com/rweng/pry-rails

该维基上还有很多其他内容,这是一个很好的资源。

答案 4 :(得分:2)

我最近写了一篇关于撬和铁轨的帖子。你可以在http://lucapette.com/pry/pry-everywhere/找到它。顺便说一下,正如戴夫已经说过的那样,你想用pry:

pry -r ./config/environment

我建议你尝试我在文章中写的内容,它的确很好用。

答案 5 :(得分:2)

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

答案 6 :(得分:2)

如果您在使用Zeus和Pry时遇到问题,请尝试添加到.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

取自here

答案 7 :(得分:1)

您的意思是./config/environment吗?

在任何情况下,我认为这与实际启动rails控制台不同,后者是reload!的来源。我在我的特定于env的配置文件中重新定义了IRB = Pry,这确保了一个完整的控制台,它就像一个魅力。

答案 8 :(得分:0)

@Rodrigo Dias的answer的更好版本。 如果您不想使用pry-rails gem,则只需在您的.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

此代码可以正确识别环境,并且不会盲目包含Rails::ConsoleMethods

来源-Github线程comment