in HOME/path_test/ I have:
load_test.rb:
require 'yaml'
cnf = YAML::load(File.open('config.yml'))
puts cnf['Hello']
config.yml:
Hello: world!!!
when in HOME/path_test/ I get as expected:
-bash-3.2$ ruby load_test.rb
world!!!
when in HOME/ (cd ..) I get
-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>'
Which is correct behavior, but not what I had wished for :)
Is there a way to load the .yml file relative to the source file, and not relative to the current working DIR??
Solution (load_Test.rb):
require 'yaml'
fn = File.dirname(File.expand_path(__FILE__)) + '/config.yml'
cnf = YAML::load(File.open(fn))
puts cnf['Hello']
question from:
https://stackoverflow.com/questions/8878389/ruby-loading-config-yaml-file-in-same-dir-as-source 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…