Ruby loading config (yaml) file in same dir as source

前端 未结 1 1110
情深已故
情深已故 2020-12-24 05:26

in HOME/path_test/ I have:

load_test.rb:

require \'yaml\'
cnf = YAML::load(File.open(\'config.yml\'))
puts cnf[\'Hello\']

config.ym

1条回答
  •  太阳男子
    2020-12-24 06:17

    You should get path of the current file by:

    cnf = YAML::load_file(File.join(File.dirname(File.expand_path(__FILE__)), 'config.yml'))
    

    EDIT:

    Since Ruby 2.0 you can simplify that and use:

    cnf = YAML::load_file(File.join(__dir__, 'config.yml'))
    

    0 讨论(0)
提交回复
热议问题