2009-05-23 116 views
25

我有一個自定義的Ruby庫目錄,我希望在Ruby執行時自動添加到Ruby的加載路徑。我知道我可以使用Ruby的-I選項,但我想知道是否有像我可以設置的環境變量,這將全局確定Ruby的加載路徑。另外,我的高級任務是在沒有root的Linux機器上安裝Ruby Gems,所以我需要在非標準位置有一個Ruby加載路徑。我已按照http://rubygems.org/read/chapter/3#page83(「在用戶目錄中安裝RubyGems」)的說明安裝了RubyGems,但gem命令沒有選擇非標準加載路徑。也許我在這裏錯過了一些明顯的東西,讓自己變得更加困難?在外部設置Ruby加載路徑

回答

36

嘗試鎬書中的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. 
+3

RUBYLIB做到了。我不確定爲什麼一個小時的谷歌搜索無法挖掘這些信息。謝謝! – 2009-05-23 14:46:35

4

確保您放置安裝bin目錄在$PATHgem 命令工作。它應該修改RUBYLIB本身,但如果沒有,請嘗試Martin's answer來解決該問題。

然後,你可以有你的寶石家園(rubygems安裝的寶石存儲在這裏)是本地的。

只需使用$GEM_HOME(或在~/.gemrc中設置),然後檢查一切是否與gem environment一致。

 
% 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

讓生活變得簡單,並安裝RVM。它會安裝你想要的任何版本的Ruby,並讓它們在它們之間切換,而且它不需要root權限。它有許多其他殺手功能,你會沉迷於使用它一段時間後。

+1

如何使用RVM在自定義位置添加庫? – 2014-05-18 17:41:02

0

謝謝!我使用@MartinCarpenter's solutionminitest運行特定/特定/單一測試方法。凡我通常與Rake::TestTasktest目錄添加到$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