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

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

原理就是先不加载 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'


出现这么大串错误了。

/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

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