启动 irb 或脚本/控制台时不执行 ~/.irbrc
这是我尝试过的:
1. gem install awesome_print
2. echo "require 'ap'" >> ~/.irbrc
3. chmod u+x ~/.irbrc
4. script/console
5. ap { :test => 'value' }
结果:
NameError: undefined local variable or method `ap' for #
Here's what I've tried:
1. gem install awesome_print
2. echo "require 'ap'" >> ~/.irbrc
3. chmod u+x ~/.irbrc
4. script/console
5. ap { :test => 'value' }
Result:
NameError: undefined local variable or method `ap' for #
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
要检查 .irbrc 中的哪些内容失败,只需将 .irbrc 作为普通 Ruby 脚本 (ruby ~/.irbrc) 执行,Ruby 将告诉您哪一行失败。它可能会抱怨 IRB 模块丢失,只需要在脚本中使用“irb”(您只在测试时需要它,而不是在实际运行 IRB 时需要)。
To check what in .irbrc is failing, just execute .irbrc as a normal Ruby script (ruby ~/.irbrc), and Ruby will tell you which line fails. It might complain that IRB module is missing, just require 'irb' in the script (you only need this when testing it, not when actually running IRB).
你的 Rails 控制台可能正在使用 Pry
根据
~/.irbrc
必须无效的建议,我将我的简化为单个puts "hi"
,尽管它在我运行了irb
,但当我运行rails console
时却没有。我终于注意到提示符是
pry(main)
。事实证明,这个 Rails 项目是使用pry-rails
gem 设置的,所以这就是控制台所使用的。自定义.pryrc
可以解决问题。Your Rails Console May Be Using Pry
Based on the suggestions that
~/.irbrc
must be invalid, I reduced mine to a singleputs "hi"
, and although it output when I ranirb
, it did not when I ranrails console
.I finally noticed that the prompt was
pry(main)
. It turned out that this Rails project is set up with thepry-rails
gem, so that's what the console uses. Customizing.pryrc
does the trick.将“require 'rubygems'”添加到我的 ~/.irbrc 文件似乎可以解决问题。
Adding "require 'rubygems'" to my ~/.irbrc file seemed to do the trick.
就我而言,它是“如果你的 ~/.irbrc 无效,它将失败
默默地。”——来自谷歌搜索
- 发现并修复了 ~/.irbrc 中的错误,一切正常
In my case it was "If your ~/.irbrc is invalid, it will fail
silently." - from Google search
- found and fixed errors in ~/.irbrc, all working fine
您需要添加
require 'rubygems'
正如 jdeseno 所写。如果您使用 Rails3 并使用 Bundler,则还需要在 Gemfile(在 :development 组中)中指定“awesome_print”gem 才能使其正常工作。
You need to add
require 'rubygems'
as jdeseno wrote.If you are on Rails3 and use Bundler, you also need to specify the 'awesome_print' gem in the Gemfile too (in the :development group) for it to work.
请检查应用程序是否使用 Pry 或 Debug。对于前者,编辑 .pryrc;后者使用 .irbrc。
您可以在存储库目录中编辑 .pryrc。扫描本地目录和 $HOME 中的点文件。
Do check whether the application is using Pry or Debug. For the former, edit .pryrc; the latter uses .irbrc.
You can edit .pryrc in your repository directory. The local directory and $HOME is scanned for the dot file.