外部设置Ruby加载路径

时间:2009-05-23 07:40:17

标签: ruby rubygems

我有一个自定义的Ruby库目录,我想在Ruby执行时自动添加到Ruby的加载路径中。我知道我可以使用Ruby的-I选项,但我想知道是否有类似我可以设置的环境变量,它将全局确定Ruby的加载路径。

顺便说一句,我的高级任务是在没有root的Linux机器上安装Ruby Gems,所以我需要在非标准位置安装Ruby加载路径。我按照http://rubygems.org/read/chapter/3#page83(“在用户目录中安装RubyGems”)的指示安装了RubyGems,但是gem命令没有获取非标准的加载路径。也许我错过了一些明显的东西,让自己变得更难?

4 个答案:

答案 0 :(得分:38)

尝试镐书中的Ruby and its world章节,特别是环境变量部分。摘录:

RUBYLIB 
  Additional search path for Ruby programs ($SAFE must be 0).
DLN_LIBRARY_PATH
  Search path for dynamically loaded modules.
RUBYLIB_PREFIX
  (Windows only) Mangle the RUBYLIB search path by adding this
  prefix to each component.

答案 1 :(得分:5)

确保已将已安装的bin目录放在$PATH的{​​{1}}中 命令工作。它应该修改gem本身,但如果没有,请尝试Martin's answer来修复它。

然后,您可以让您的宝石回家(存储rubygems安装的宝石)是本地的。

只需使用RUBYLIB(或在$GEM_HOME中进行设置),然后检查所有内容是否都在使用~/.gemrc

% mkdir ~/.gems
% export GEM_HOME=~/.gems
% gem help environment

Usage: gem environment [arg] [options]

  Common Options:
    -h, --help                       Get help on this command
    -V, --[no-]verbose               Set the verbose level of output
    -q, --quiet                      Silence commands
        --config-file FILE           Use this config file instead of default
        --backtrace                  Show stack backtrace on errors
        --debug                      Turn on Ruby debugging


  Arguments:
    packageversion  display the package version
    gemdir          display the path where gems are installed
    gempath         display path used to search for gems
    version         display the gem format version
    remotesources   display the remote gem servers
           display everything

  Summary:
    Display information about the RubyGems environment

  Description:
    The RubyGems environment can be controlled through command line arguments,
    gemrc files, environment variables and built-in defaults.

    Command line argument defaults and some RubyGems defaults can be set in
    ~/.gemrc file for individual users and a /etc/gemrc for all users.  A gemrc
    is a YAML file with the following YAML keys:

      :sources: A YAML array of remote gem repositories to install gems from
      :verbose: Verbosity of the gem command.  false, true, and :really are the
                levels
      :update_sources: Enable/disable automatic updating of repository metadata
      :backtrace: Print backtrace when RubyGems encounters an error
      :bulk_threshold: Switch to a bulk update when this many sources are out of
                       date (legacy setting)
      :gempath: The paths in which to look for gems
      gem_command: A string containing arguments for the specified gem command

    Example:

      :verbose: false
      install: --no-wrappers
      update: --no-wrappers

    RubyGems' default local repository can be overriden with the GEM_PATH and
    GEM_HOME environment variables.  GEM_HOME sets the default repository to
    install into.  GEM_PATH allows multiple local repositories to be searched
    for
    gems.

    If you are behind a proxy server, RubyGems uses the HTTP_PROXY,
    HTTP_PROXY_USER and HTTP_PROXY_PASS environment variables to discover the
    proxy server.

    If you are packaging RubyGems all of RubyGems' defaults are in
    lib/rubygems/defaults.rb.  You may override these in
    lib/rubygems/defaults/operating_system.rb

答案 2 :(得分:2)

让生活更轻松并安装RVM。它将安装您想要的任何版本的Ruby,并允许您在它们之间切换,并且它不需要root访问权限。它有许多其他的杀手功能,你会在使用它一段时间后上瘾。

答案 3 :(得分:0)

谢谢!我使用@MartinCarpenter's solution运行minitest的特定/特定/单一测试方法。我通常将test目录添加到带有Rake::TestTask$LOAD_PATH,例如t.libs << 'test',我可以使用命令行执行此操作,如下所示:

RUBYLIB=test ruby test/user_test.rb --name test_create

我将test添加到$LOAD_PATH,因为user_test.rb调用require 'test_helper'来加载lib/test_helper.rb