如何从另一个目录中获取 ruby​​ 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16856243/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:05:43  来源:igfitidea点击:

How to require a ruby file from another directory

ruby-on-railsrubyruby-on-rails-3ruby-on-rails-3.2require

提问by thedarkknight228

I am trying to require a rake file that I have created inside another file I have. These two files are inside two different directories. I have require at the top of my first file with the name of the second file inside the quotes after the require. It is telling me that it can't load such file. Does that mean because its in different directory it can't find it? I tried sticking in the full path to the second file but it still can't load the file. Does anyone know how I can load the second file into the first?

我试图需要一个我在另一个文件中创建的 rake 文件。这两个文件位于两个不同的目录中。我在第一个文件的顶部有 require ,在 require 之后的引号内有第二个文件的名称。它告诉我它无法加载这样的文件。这是否意味着因为它在不同的目录中找不到它?我试图坚持到第二个文件的完整路径,但它仍然无法加载文件。有谁知道如何将第二个文件加载到第一个文件中?

Thanks in advance

提前致谢

回答by Charles Caldwell

requirewill search for files in only a set of locations referred to as the "Load Path." You can view the load path by using the global variable $LOAD_PATHin a script or irb session. If it is not in the load path, it won't find it.

require将仅在称为“加载路径”的一组位置中搜索文件。您可以通过$LOAD_PATH在脚本或 irb 会话中使用全局变量来查看加载路径。如果它不在加载路径中,它就不会找到它。

Ruby 1.9 introduced require_relativewhich searches using the current file's location as a starting point.

Ruby 1.9 引入了require_relative使用当前文件位置作为起点的搜索。

# Will search $LOAD_PATH for file. 
require 'test/unit'
# Notice the '/' which tells it to look in the 
# 'test' folder for a file named 'unit.rb'

# Will look in current folder of file
require_relative 'my_folder/my_file'
# Will search in 'my_folder' for the file 'my_file.rb'

Note that require_relativewill not work in irb.

请注意,这require_relative在 irb 中不起作用。

Also note, that if you really want to use require, you can start your script by adding a location to the $LOAD_PATHvariable.

另请注意,如果您真的想使用require,则可以通过向$LOAD_PATH变量添加位置来启动脚本。

$LOAD_PATH << File.join('users', 'yourusername', 'your_folder')
# or
$LOAD_PATH << File.dirname(__FILE__)
# The second one enables you to move the file around on your
# system and still operate correctly
require 'my_file'

Here's some additional documentation from Ruby-Doc:

以下是 Ruby-Doc 的一些附加文档:

回答by Bijendra

From the docs: It suggests to use require relative. Docs

来自文档:它建议使用 require relative。文档

require_relativecomplements the builtin method require by allowing you to load a file that is relative to the file containing the require_relativestatement.

require_relative通过允许您加载与包含require_relative语句的文件相关的文件来补充内置方法 require 。

When you use requireto load a file, you are usually accessing functionality that has been properly installed, and made accessible, in your system. require does not offer a good solution for loading files within the project's code. This may be useful during a development phase, for accessing test data, or even for accessing files that are "locked" away inside a project, not intended for outside use.

当您require用来加载文件时,您通常会访问系统中已正确安装并使其可访问的功能。require 没有为在项目代码中加载文件提供一个好的解决方案。这在开发阶段可能很有用,可用于访问测试数据,甚至可用于访问“锁定”在项目内部、不供外部使用的文件。

For example, if you have unit test classes in the "test" directory, and data for them under the test "test/data" directory, then you might use a line like this in a test case:

例如,如果您在“test”目录中有单元测试类,而在测试“test/data”目录下有它们的数据,那么您可以在测试用例中使用这样的一行:

require_relative "data/customer_data_1"

回答by BladeMight

Also possible to do so:

也可以这样做:

require './something.rb'

from current directory to include.

从当前目录包含。

回答by Alexandr

For example, you have files:

例如,您有文件:

chess.rb and spec/test.rb

chess.rb 和 spec/test.rb

And you want to require the file in spec/test.rb. Use:

并且您想在 spec/test.rb 中要求该文件。用:

require_relative '../test.rb'

with two dots. With one dot './' you will have an error "cannot load such file"

带两个点。使用一个点“./”,您将收到错误“无法加载此类文件”