Rails pry 代替 Rails console 的 irb,不用加进 Gemfile

doitian · 2012年04月09日 · 最后由 ranmocy 回复于 2013年03月23日 · 8424 次阅读

原理就是先不加载 Bundler,用 rubygems 把 pry awesome_print 等 gems 加进来以后来把 rails app 加进来。把 Rails::Console::IRB 设置成 Pry, Hack下 ARGV,就可以交给 rails/commands 处理了

已经包装成 gem rails-console-pry,安装不走 bundler,如果用了 rvm gemset,需要安装到 gemset 中。顺带可以安装些 pry plugins

gem install rails-console-pry pry-doc awesome_print

然后在 rails app 根目录直接

rails-console-pry

或者进 production 环境

rails-console-pry production

需要在 Bundler 之前加载插件(就是指不在 Gemfile 中的插件)用 -r 选项

rails-console-pry -r pry-doc -r awesome_print

最好是加个 alias

alias rpry='rails-cosnole-pry -r pry-doc -r awesome_print'


共收到 17 条回复

出现这么大串错误了。

/Users/alpha/.rvm/gems/ruby-1.9.3-head/gems/pry-rails-0.1.6/lib/pry-rails.rb:1:in `require': cannot load such file -- pry-rails/version (LoadError)
    from /Users/alpha/.rvm/gems/ruby-1.9.3-head/gems/pry-rails-0.1.6/lib/pry-rails.rb:1:in `<top (required)>'
    from /Users/alpha/.rvm/gems/ruby-1.9.3-head/gems/rails-console-pry-0.0.4/bin/rails-console-pry:37:in `require'
    from /Users/alpha/.rvm/gems/ruby-1.9.3-head/gems/rails-console-pry-0.0.4/bin/rails-console-pry:37:in `<top (required)>'
    from /Users/alpha/.rvm/gems/ruby-1.9.3-head/bin/rails-console-pry:19:in `load'
    from /Users/alpha/.rvm/gems/ruby-1.9.3-head/bin/rails-console-pry:19:in `<main>'

#1楼 @AlphaLiu 手动编辑下 /Users/alpha/.rvm/gems/ruby-1.9.3-head/gems/rails-console-pry-0.0.4/bin/rails-console-pry:37

在37行之前插入这一行看能解决吗?

$:.push File.join(pry_rails_path.full_gem_path, 'lib')

#2楼 @doitian 加入这一行就解决了,哈哈!

#3楼 @AlphaLiu 更新到 0.0.5 了

一直是加到 Gemfile 里用的,加个staging env 进去就更好了

在rails项目下输入rails-console-pry -r pry-doc -r awesome_print,看起来结果跟rails c的一样啊?有什么特色?我看railscast下的pry可以用ls命令,这个不行?

#6楼 @AlphaLiu 就是用 pry 代替了 irb,可以 ls, cd, edit, edit-method 等等.

1.9.3p134 :005 > ls NameError: undefined local variable or method ls' for main:Object from (irb):5 from /Users/alpha/.rvm/gems/ruby-1.9.3-head/gems/railties-3.2.2/lib/rails/commands/console.rb:47:instart' from /Users/alpha/.rvm/gems/ruby-1.9.3-head/gems/railties-3.2.2/lib/rails/commands/console.rb:8:in start' from /Users/alpha/.rvm/gems/ruby-1.9.3-head/gems/railties-3.2.2/lib/rails/commands.rb:41:in' from /Users/alpha/.rvm/gems/ruby-1.9.3-head/gems/rails-console-pry-0.0.4/bin/rails-console-pry:47:in require' from /Users/alpha/.rvm/gems/ruby-1.9.3-head/gems/rails-console-pry-0.0.4/bin/rails-console-pry:47:in' from /Users/alpha/.rvm/gems/ruby-1.9.3-head/bin/rails-console-pry:19:in load' from /Users/alpha/.rvm/gems/ruby-1.9.3-head/bin/rails-console-pry:19:in

'

奇怪,ls命令之类出现这个错误?

这个好像是 pry 没有加进来,试试 rails-console-pry 0.0.5。我刚测试了下 rails 3.2.2 , 3.2.3 都没问题

@doitian @AlphaLiu @vkill 不是有 pry-rails 的么..直接在 gemfile 里面加上 gem 'pry-rails', :group => :development 就好了

当然最好再加上 pry-doc...

#11楼 @ruohanc 用于pry不在Gemfile里的场景,比如pry加在development group,在production环境里想使用。

#8楼 @AlphaLiu 所有的shell命令前都要加一个"." 例如".ls",".clear",".pwd" etc..

我是这样做的:

# in .oh-my-zsh/custom/aliases.zsh
pry-rails () {
        pry -I config -I ~/src/misc -e "require 'pry-rails'"
}
# ~/src/misc/pry-rails.rb
require 'environment'
require "rails/console/app"
require "rails/console/helpers"
TOPLEVEL_BINDING.eval('self').extend ::Rails::ConsoleMethods
ActiveRecord::Base.logger = Logger.new(STDOUT)

这个有意思,而且很ruby

@doitian 我用的0.0.5也报错了

/home/ror/.rvm/gems/ruby-1.9.3-p125/gems/pry-rails-0.2.1/lib/pry-rails.rb:3:in require': cannot load such file -- pry (LoadError) from /home/ror/.rvm/gems/ruby-1.9.3-p125/gems/pry-rails-0.2.1/lib/pry-rails.rb:3:in' from /home/ror/.rvm/gems/ruby-1.9.3-p125/gems/rails-console-pry-0.0.5/bin/rails-console-pry:38:in require' from /home/ror/.rvm/gems/ruby-1.9.3-p125/gems/rails-console-pry-0.0.5/bin/rails-console-pry:38:in' from /home/ror/.rvm/gems/ruby-1.9.3-p125/bin/rails-console-pry:19:in load' from /home/ror/.rvm/gems/ruby-1.9.3-p125/bin/rails-console-pry:19:in

' from /home/ror/.rvm/gems/ruby-1.9.3-p125/bin/ruby_noexec_wrapper:14:in eval' from /home/ror/.rvm/gems/ruby-1.9.3-p125/bin/ruby_noexec_wrapper:14:in'

我有装pry,pry-doc 等的 环境: rails 3.2.8 rvm 1.14.12 ruby 1.9.3p125 (2012-02-16 revision 34643) [x86_64-linux] ubuntu 12.04 server

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册