In Ruby, how do I specify a file in another directory as an input?

こ雲淡風輕ζ 提交于 2019-12-12 22:19:33

问题


This probably has a simple answer, but I'm working on a test suite that requires an input file that is in a different folder. I'd like to use a relative path, like this:

@graph = Graph.new('../lib/test_input.txt')

But Ruby doesn't like that. What's the best way to use a relative file path like that?

Thanks


回答1:


If you mean relative to the current file, you'll probably want something like:

@graph = Graph.new(File.expand_path(__FILE__, "../lib/test_input.txt"))

If you mean relative to the current directory, you'll probably want something like:

@graph = Graph.new(File.expand_path(Dir.pwd, "../lib/test_input.txt"))

bonus link!



来源:https://stackoverflow.com/questions/14432498/in-ruby-how-do-i-specify-a-file-in-another-directory-as-an-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!