代码之家  ›  专栏  ›  技术社区  ›  Adrian

当我用sudo运行ruby时,为什么ruby的“$:”或“$LOAD\u PATH”会改变?

  •  0
  • Adrian  · 技术社区  · 14 年前

    require 宝石,鲁比说找不到。这是因为 $:

    $ ruby -e "puts $:"
    /opt/local/lib/ruby/gems/1.9.1/gems/activesupport-3.0.0.beta4/lib
    /opt/local/lib/ruby/gems/1.9.1/gems/cgi_multipart_eof_fix-2.5.0/lib
    /opt/local/lib/ruby/gems/1.9.1/gems/daemons-1.1.0/lib
    /opt/local/lib/ruby/gems/1.9.1/gems/eventmachine-0.12.10/lib
    /opt/local/lib/ruby/gems/1.9.1/gems/fastthread-1.0.7/lib
    /opt/local/lib/ruby/gems/1.9.1/gems/fastthread-1.0.7/ext
    /opt/local/lib/ruby/gems/1.9.1/gems/fastthread-1.0.7/bin
    /opt/local/lib/ruby/gems/1.9.1/gems/gem_plugin-0.2.3/bin
    /opt/local/lib/ruby/gems/1.9.1/gems/gem_plugin-0.2.3/lib
    /opt/local/lib/ruby/gems/1.9.1/gems/json_pure-1.4.3/bin
    /opt/local/lib/ruby/gems/1.9.1/gems/json_pure-1.4.3/lib
    /opt/local/lib/ruby/gems/1.9.1/gems/mongrel-1.1.5/bin
    /opt/local/lib/ruby/gems/1.9.1/gems/mongrel-1.1.5/lib
    /opt/local/lib/ruby/gems/1.9.1/gems/msgpack-0.4.3/lib
    /opt/local/lib/ruby/gems/1.9.1/gems/nestful-0.0.2/lib
    /opt/local/lib/ruby/gems/1.9.1/gems/nokogiri-1.4.2/bin
    /opt/local/lib/ruby/gems/1.9.1/gems/nokogiri-1.4.2/lib
    /opt/local/lib/ruby/gems/1.9.1/gems/num_to_bytes-1.4.1/ext
    /opt/local/lib/ruby/gems/1.9.1/gems/num_to_bytes-1.4.1/bin
    /opt/local/lib/ruby/gems/1.9.1/gems/open4-1.0.1/lib
    /opt/local/lib/ruby/gems/1.9.1/gems/rack-1.2.1/bin
    /opt/local/lib/ruby/gems/1.9.1/gems/rack-1.2.1/lib
    /opt/local/lib/ruby/gems/1.9.1/gems/rake-0.8.7/bin
    /opt/local/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib
    /opt/local/lib/ruby/gems/1.9.1/gems/ruby-processing-1.0.9/bin
    /opt/local/lib/ruby/gems/1.9.1/gems/ruby-processing-1.0.9/lib
    /opt/local/lib/ruby/gems/1.9.1/gems/ruby_parser-2.0.4/bin
    /opt/local/lib/ruby/gems/1.9.1/gems/ruby_parser-2.0.4/lib
    /opt/local/lib/ruby/gems/1.9.1/gems/rubyforge-2.0.4/bin
    /opt/local/lib/ruby/gems/1.9.1/gems/rubyforge-2.0.4/lib
    /opt/local/lib/ruby/gems/1.9.1/gems/sexp_processor-3.0.4/lib
    /opt/local/lib/ruby/gems/1.9.1/gems/sinatra-1.0/lib
    /opt/local/lib/ruby/gems/1.9.1/gems/thin-1.2.7/bin
    /opt/local/lib/ruby/gems/1.9.1/gems/thin-1.2.7/lib
    /opt/local/lib/ruby1.9/site_ruby/1.9.1
    /opt/local/lib/ruby1.9/site_ruby/1.9.1/i386-darwin10
    /opt/local/lib/ruby1.9/site_ruby
    /opt/local/lib/ruby1.9/vendor_ruby/1.9.1
    /opt/local/lib/ruby1.9/vendor_ruby/1.9.1/i386-darwin10
    /opt/local/lib/ruby1.9/vendor_ruby
    /opt/local/lib/ruby1.9/1.9.1
    /opt/local/lib/ruby1.9/1.9.1/i386-darwin10
    .
    $ sudo ruby -e "puts $:"
    /opt/local/lib/ruby1.9/site_ruby/1.9.1
    /opt/local/lib/ruby1.9/site_ruby/1.9.1/i386-darwin10
    /opt/local/lib/ruby1.9/site_ruby
    /opt/local/lib/ruby1.9/vendor_ruby/1.9.1
    /opt/local/lib/ruby1.9/vendor_ruby/1.9.1/i386-darwin10
    /opt/local/lib/ruby1.9/vendor_ruby
    /opt/local/lib/ruby1.9/1.9.1
    /opt/local/lib/ruby1.9/1.9.1/i386-darwin10
    .
    

    为什么会这样?

    1 回复  |  直到 14 年前
        1
  •  3
  •   amphetamachine    14 年前

    尝试 sudo -E ruby -e "puts $:" .

    sudo 手册页:

       -E          The -E (preserve environment) option will override the env_reset option in sudoers(5)).
                   It is only available when either the matching command has the SETENV tag or the setenv
                   option is set in sudoers(5).