在 ruby 中链接到外部文件?
抱歉,如果这个问题很简单,但我无法在任何地方找到答案。例如,如果您想要的文件与您正在编写的文件位于同一文件夹中,那么如何在 ruby 脚本中引用外部 ruby 文件? 提前致谢。
Sorry if this question is very easy, but I can't find the answer anywhere. How do you refer to an external ruby file in a ruby script if, for example, the file you want is in the same folder as the one you are writing?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您只需执行这样的要求
require "filename"
即可。 Ruby 将查找 $LOAD_PATH 变量中的每个路径来查找文件。看一下 $LOAD_PATH,您应该得到类似这样的信息:
您可以看到最后一个条目是当前目录。
您还可以专门指定一个路径,例如
require "lib/filename.rb"
You just do a require like this
require "filename"
. Ruby will look in each of the paths in the$LOAD_PATH
variable to find the file.Have a look at $LOAD_PATH and you should get something like this:
You can see that the last entry is the current directory.
You can also give a path specifically like
require "lib/filename.rb"
例如,如果您有两个文件“file0.rb”和“file1.rb”,并且想要包含“file1.rb”中的“file0.rb”(并且这两个文件位于同一文件夹中),则您的“file1.rb”将包含在“file1.rb”中。 rb' 应该有以下声明:
For example, if you have two files, 'file0.rb' and 'file1.rb' and want to include 'file0.rb' from 'file1.rb' (and both files are in the same folder), your 'file1.rb' should have following statement: