ruby 默认情况下“需要”查找的路径是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9474299/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What are the paths that "require" looks up by default?
提问by Mark Provan
In Ruby, I have been told that when doing
在 Ruby 中,有人告诉我在做
require "some_file"
Ruby will look for the file in certain places.
Ruby 会在某些地方寻找该文件。
I know that it looks for some_file.rb, but where does it look for it by default?
我知道它会寻找some_file.rb,但它默认在哪里寻找它?
回答by Daniel Pittman
It depends on your platform, and how Ruby was compiled, so there is no "the" answer to this. You can find out by running:
这取决于您的平台,以及 Ruby 的编译方式,因此对此没有“答案”。您可以通过运行找到:
ruby -e 'puts $:'
Generally, though, you have the standard, site, and vendor Ruby library paths, including an arch, version, and general directory under each.
不过,通常情况下,您有标准、站点和供应商 Ruby 库路径,每个路径下都包括架构、版本和常规目录。
回答by Brian Hempel
Ruby looks in all the paths specified in the $LOAD_PATHarray.
Ruby 会查看$LOAD_PATH数组中指定的所有路径。
You can also add a directory to search like so:
您还可以添加一个目录进行搜索,如下所示:
$LOAD_PATH.unshift File.expand_path('../path/from/this/file/to/another/directory', __FILE__)
回答by mighq
additional paths can be specified by setting RUBYLIB environment variable
可以通过设置 RUBYLIB 环境变量来指定其他路径
回答by Perry
The $LOAD_PATH global variable (also named $:) contains the list of directories that are searched.
$LOAD_PATH 全局变量(也称为 $:)包含要搜索的目录列表。
See: http://www.ruby-doc.org/core-1.9.3/Kernel.html#method-i-require
请参阅:http: //www.ruby-doc.org/core-1.9.3/Kernel.html#method-i-require
回答by ericraio
require(string) => true or false
要求(字符串)=> 真或假
Ruby tries to load the library named string, returning true if successful. If the filename does not resolve to an absolute path, it will be searched for in the directories listed in $:. If the file has the extension ".rb", it is loaded as a source file; if the extension is ".so", ".o", or ".dll", or whatever the default shared library extension is on the current platform, Ruby loads the shared library as a Ruby extension. Otherwise, Ruby tries adding ".rb", ".so", and so on to the name. The name of the loaded feature is added to the array in $:.
Ruby 尝试加载名为 string 的库,如果成功则返回 true。如果文件名未解析为绝对路径,则会在 $: 中列出的目录中搜索。如果文件的扩展名为“.rb”,则作为源文件加载;如果扩展名是“.so”、“.o”或“.dll”,或者当前平台上的任何默认共享库扩展名,Ruby 会将共享库作为 Ruby 扩展名加载。否则,Ruby 会尝试在名称中添加“.rb”、“.so”等。加载的特征的名称被添加到 $: 中的数组中。
回答by Mecki
When calling rubyon command line, you can provide additional search paths using the -Iargument. Compare the output of
ruby在命令行上调用时,您可以使用-I参数提供其他搜索路径。比较输出
$ ruby -e 'puts $:'
with the output of
与输出
$ ruby -I /tmp -e 'puts $:'
note how the second one lists /tmpas an option. You can use multiple -Ito add multiple path.
注意第二个是如何/tmp作为选项列出的。您可以使用 multiple-I来添加多个路径。
You can also use it with the shebang:
您还可以将它与 shebang 一起使用:
#!/usr/bin/ruby -I /tmp -I /usr/local/lib/ruby

