【发布时间】:2012-02-11 07:09:52
【问题描述】:
在 HOME/path_test/ 我有:
load_test.rb:
require 'yaml'
cnf = YAML::load(File.open('config.yml'))
puts cnf['Hello']
config.yml:
Hello: world!!!
当在 HOME/path_test/ 时,我得到了预期:
-bash-3.2$ ruby load_test.rb
world!!!
当在 HOME/ (cd ..) 我得到
-bash-3.2$ ruby path_test/load_test.rb
path_test/load_test.rb:3:in `initialize': No such file or directory - config.yml (Errno::ENOENT)
from path_test/load_test.rb:3:in `open'
from path_test/load_test.rb:3:in `<main>'
这是正确的行为,但不是我所希望的:)
有没有办法加载相对于源文件而不是相对于当前工作目录的 .yml 文件??
解决方案(load_Test.rb):
require 'yaml'
fn = File.dirname(File.expand_path(__FILE__)) + '/config.yml'
cnf = YAML::load(File.open(fn))
puts cnf['Hello']
【问题讨论】:
标签: ruby path yaml configuration-files