In Ruby, how do I specify a file in another direct

2019-07-15 13:59发布

问题:

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!