为什么我不能加载Nokogiri?

时间:2013-05-31 06:26:27

标签: ruby rvm nokogiri

我通过运行来安装Nokogiri没有任何问题:

$ sudo gem install nokogiri
Building native extensions.  This could take a while...
Successfully installed nokogiri-1.5.9
1 gem installed
Installing ri documentation for nokogiri-1.5.9...
Installing RDoc documentation for nokogiri-1.5.9...

当我运行nokogiri.rb时:

#!/usr/bin/ruby -w

require 'nokogiri'

puts "Current directory is: #{ Dir.pwd }"
Dir.chdir("/home/askar/xml_files1") do |dirname|

  puts "Now in: #{ Dir.pwd }"
  xml_files = Dir.glob("ShipmentRequest*.xml")

  if xml_files.empty?
    puts "#{ dirname } is empty."
  else
    xml_files.each do |file|
      doc = Nokogiri::XML(open(file))
      # ... do something with the doc ...
    end
  end
end

我收到了错误:

$ ruby nokogiri.rb 
/home/askar/.rvm/rubies/ruby-1.9.3-p429/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- nokogiri (LoadError)
  from /home/askar/.rvm/rubies/ruby-1.9.3-p429/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'

我使用Ruby 1.9.3,但由于某种原因它引用了Ruby 1.9.1。这可能是原因吗?

1 个答案:

答案 0 :(得分:7)

如果仔细观察,路径以/home/askar/.rvm/rubies/ruby-1.9.3-p429开头,因此加载路径应该是正确的。

您的问题是您使用了sudo来为系统ruby执行gem安装。不用sudo再试一次,只需

gem install nokogiri

为当前的rvm ruby​​安装gems。