Ruby需要加载错误

时间:2012-06-08 19:03:59

标签: ruby gem require

我想要两个宝石:json和rinku。

我正在使用托管的Linux机器,并且没有root / sudo访问权限,并且最终在我的主目录中安装了gem:

-jailshell-3.2$ gem which json
/home/cygnetrc/ruby/gems/gems/json-1.7.3/lib/json.rb
-jailshell-3.2$ gem which rinku
/home/cygnetrc/ruby/gems/gems/rinku-1.5.1/lib/rinku.rb

Ruby版本是:

-jailshell-3.2$ ruby --version
ruby 1.8.7 (2011-02-18 patchlevel 334) [x86_64-linux]

RubyGems环境是:

  - RUBYGEMS VERSION: 1.8.11
  - RUBY VERSION: 1.8.7 (2011-02-18 patchlevel 334) [x86_64-linux]
  - INSTALLATION DIRECTORY: /home/cygnetrc/ruby/gems
  - RUBY EXECUTABLE: /usr/bin/ruby
  - EXECUTABLE DIRECTORY: /home/cygnetrc/ruby/gems/bin
  - RUBYGEMS PLATFORMS:
    - ruby
    - x86_64-linux
  - GEM PATHS:
     - /home/cygnetrc/ruby/gems
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :benchmark => false
     - :backtrace => false
     - :bulk_threshold => 1000
     - "gempath" => []
     - "gem" => "--remote --gen-rdoc --run-tests"
     - "gemhome" => "/home/cygnetrc/ruby/gems"
     - "rdoc" => "--inline-source --line-numbers"
  - REMOTE SOURCES:
     - http://rubygems.org/


-jailshell-3.2$ ruby -e 'puts $:'
/usr/lib/ruby/site_ruby/1.8
/usr/lib/ruby/site_ruby/1.8/x86_64-linux
/usr/lib/ruby/site_ruby
/usr/lib/ruby/vendor_ruby/1.8
/usr/lib/ruby/vendor_ruby/1.8/x86_64-linux
/usr/lib/ruby/vendor_ruby
/usr/lib/ruby/1.8
/usr/lib/ruby/1.8/x86_64-linux
.

我试图使用以下内容包含宝石:

-jailshell-3.2$ head gettweets.rb 
#!/usr/bin/ruby 

require 'rubygems'
require 'json'
require 'net/https'
require 'uri'
require 'rinku'

但结束时:

-jailshell-3.2$ ./gettweets.rb 
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in `gem_original_require': no such file to load -- json (LoadError)
        from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in `require'
        from ./gettweets.rb:7

我广泛地环顾四周,发现了很多关于各种不同路径变量的信息,比如RUBYOPTS,但似乎没有什么对我有用。

为了使问题更加复杂,我将从一个cron作业调用脚本,所以无论我使用它的解决方案都不能依赖于改变我的主目录中的任何内容,如我的.profile。

我把头发拉出来 - 有人可以帮忙吗?


更新:

感谢您的反馈!根据你的一些建议,我读了GEM_PATH,如果我将它设置并导出为shell环境变量,那么它是有效的。但是,由于我想从cron运行脚本,我需要能够在脚本本身内设置它。我试过这样做,但它似乎没有起作用。还有什么进一步的想法吗?

-jailshell-3.2$ ruby -I ~/ruby/gems ./gettweets.rb 
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in `gem_original_require': no such file to load -- json (LoadError)
        from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in `require'
        from ./gettweets.rb:4
-jailshell-3.2$ head -20 ./gettweets.rb 
#!/usr/bin/ruby 

require 'rubygems'
require 'json'
require 'net/https'
require 'uri'
require 'rinku'
require 'optparse'

OptionParser.new do |o|
  o.on('-d', "Turn on debugging messages") { |b| $debug = b }
  o.on('-h', "Print this help screen") { puts o; exit }
  o.parse!
end

puts "[*] Starting..." if $debug

# Setenv to catch json gem
ENV['GEM_PATH'] = '~/ruby/gems'
puts "ENV " + ENV['GEM_PATH'] if $debug
-jailshell-3.2$ export GEM_PATH=~/ruby/gems
-jailshell-3.2$ ruby ./gettweets.rb  -d
[*] Starting...
ENV ~/ruby/gems
[*] Filepath = /home/cygnetrc/www/drupal/tweets.html
The queens of the women's single sculls - a look back at the Olympic history: http://t.co/d0YmNr40 #rowing #wr #London2012 #Olympics<br />
<em>Fri Jun 08 16:05:38</em><br />
1 week until #WRCMunich! NZL's Storm Uru will be racing the LM2x with @petertaylorNZ. Here, Uru talks about teamwork: http://t.co/NvjRgtwW<br />
<em>Fri Jun 08 14:38:02</em><br />
The 2012 @Paralympic Games Qualification Process is complete: http://t.co/D6DRG0bO #rowing #adaptive #Paralympic #London2012 #wr<br />
<em>Fri Jun 08 13:23:14</em><br />
true
[*] Finished
-jailshell-3.2$ 

1 个答案:

答案 0 :(得分:0)

我遇到了一个类似的问题,通过Apache mod_cgi运行Ruby。环境变量未设置,但CGI脚本以用户而非Apache的形式运行。这是我的解决方法。

#!/bin/bash
source "/home/username/.bashrc"
rubyscript.cgi

但是,我想要一个真正的方法来添加到Ruby脚本本身的gems路径以避免这样的变通方法并发现:

ENV["HOME"] = "/home/user"
ENV["GEM_HOME"] = ENV["HOME"] + "/ruby/gems"
ENV["GEM_PATH"] = ENV["GEM_HOME"] + ":/usr/lib/ruby/gems/1.8"
ENV["GEM_CACHE"] = "GEM_HOME/cache"
ENV["PATH"] = ENV["PATH"] + ":" + ENV["HOME"] + "/ruby/gems/bin"

这可以放在Ruby脚本的头部或放在一个文件中,并在开头“必需”,即environment.rb:

ENV["HOME"] = "/home/user"
ENV["GEM_HOME"] = ENV["HOME"] + "/ruby/gems"
ENV["GEM_PATH"] = ENV["GEM_HOME"] + ":/usr/lib/ruby/gems/1.8"
ENV["GEM_CACHE"] = "GEM_HOME/cache"
ENV["PATH"] = ENV["PATH"] + ":" + ENV["HOME"] + "/ruby/gems/bin"

ruby​​.cgi:

#!/usr/bin/ruby
require "environment"
require "rubygems"
require "json"
puts "Content-type: text/json"
puts ""
puts "{'bacon':['yum','tasty']}"