在Ruby中,如何将另一个目录中的文件指定为输入?

use*_*934 2 ruby directory relative-path

这可能有一个简单的答案,但我正在研究一个需要输入文件位于不同文件夹的测试套件.我想使用相对路径,如下所示:

@graph = Graph.new('../lib/test_input.txt')
Run Code Online (Sandbox Code Playgroud)

但Ruby并不喜欢这样.使用这样的相对文件路径的最佳方法是什么?

谢谢

Jer*_*odi 6

如果您的意思是相对于当前文件,您可能需要以下内容:

@graph = Graph.new(File.expand_path(__FILE__, "../lib/test_input.txt"))
Run Code Online (Sandbox Code Playgroud)

如果你的意思是相对于当前目录,你可能想要这样的东西:

@graph = Graph.new(File.expand_path(Dir.pwd, "../lib/test_input.txt"))
Run Code Online (Sandbox Code Playgroud)

奖金链接!