问题描述
这使我发疯。我可以通过irb加载宝石:
This one is driving me nuts. I can load a gem via irb:
steve@server:/var/www/listings$ irb
irb(main):001:0> Gem.path
=> ["/home/steve/.gem/ruby/1.9.1", "/usr/local/ruby/lib/ruby/gems/1.9.1"]
irb(main):002:0> require 'nokogiri'
=> true
但是我无法通过Rails控制台加载它:
But I can't load it through the rails console:
irb(main):001:0> Gem.path
=> ["/home/steve/.gem/ruby/1.9.1", "/usr/local/ruby/lib/ruby/gems/1.9.1"]
irb(main):002:0> require 'nokogiri'
=> false
已安装gem(nokogiri)
The gem (nokogiri) is installed
steve@server:/var/www/listings$ gem which nokogiri
/usr/local/ruby/lib/ruby/gems/1.9.1/gems/nokogiri-1.4.3.1/lib/nokogiri.rb
并且捆绑包同意
steve@server:/var/www/listings$ bundle show nokogiri
/usr/local/ruby/lib/ruby/gems/1.9.1/gems/nokogiri-1.4.3.1
但是,当然,耙规格会因
But, of course, rake spec fails with
/usr/local/ruby/lib/ruby/gems/1.9.1/gems/activesupport-3.0.1/lib/active_support/dependencies.rb:304:in `rescue in depend_on': No such file to load -- Nokogiri (LoadError)
其他环境信息:
steve@server:/var/www/listings$ ruby --version
ruby 1.9.2p0 (2010-08-18 revision 29036) [i686-linux]
steve@server:/var/www/listings$ rails --version
Rails 3.0.1
steve@server:/var/www/listings$ gem env
RubyGems Environment:
- RUBYGEMS VERSION: 1.3.7
- RUBY VERSION: 1.9.2 (2010-08-18 patchlevel 0) [i686-linux]
- INSTALLATION DIRECTORY: /usr/local/ruby/lib/ruby/gems/1.9.1
- RUBY EXECUTABLE: /usr/local/ruby/bin/ruby
- EXECUTABLE DIRECTORY: /usr/local/ruby/bin
- RUBYGEMS PLATFORMS:
- ruby
- x86-linux
- GEM PATHS:
- /usr/local/ruby/lib/ruby/gems/1.9.1
- /home/steve/.gem/ruby/1.9.1
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :benchmark => false
- :backtrace => false
- :bulk_threshold => 1000
- REMOTE SOURCES:
- http://rubygems.org/
任何建议?
编辑
顺便说一句,Nokogiri在Gemfile和捆绑软件安装完成,没有任何投诉。
By the way, Nokogiri is in the Gemfile and bundle install completes without complaint.
这是我正在从Windows迁移到Ubuntu的一个项目。在Windows上,它工作正常(非常好!),所以我很确定这是环境问题。
This is a project I'm moving from windows to Ubuntu. On windows it's working fine (oddly enough!) so I'm pretty sure it's an environment thing.
推荐答案
require如果返回false该文件已被加载。通过连续两次执行require语句,在irb会话中进行尝试。第二个将返回false:
require returns false if that file has already been loaded. Try it out in your irb session by performing the require statement twice in a row. The second one will return false:
irb(main):001:0> require 'nokogiri'
=> true
irb(main):002:0> require 'nokogiri'
=> false
如果找不到文件,则require将引发LoadError。
If the file could not be found, require will raise a LoadError instead.
您的异常消息(没有要加载的文件-Nokogiri),似乎是在需要 Nokogiri而不是 nokogiri,这可能是一个问题在区分大小写的操作系统上。
Your exception message (No such file to load -- Nokogiri), makes it seem like something is requiring 'Nokogiri' instead of 'nokogiri', which might be a problem on a case-sensitive operating system.
这篇关于宝石在irb中加载,但不在控制台中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!