问题
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