English 中文(简体)
Ruby config (yaml)文档与来文方相同。
原标题:Ruby loading config (yaml) file in same dir as source

in HOME/path_test/ I have:

装载量:试验。

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

config.yml:

Hello: world!!!

艾滋病毒/艾滋病 我照预期的那样:

-bash-3.2$ ruby load_test.rb 
world!!!

当时,我收到有关文件。

-bash-3.2$ ruby path_test/load_test.rb 
path_test/装载量:试验。3:in `initialize : No such file or directory - config.yml     (Errno::ENOENT)
    from path_test/装载量:试验。3:in `open 
    from path_test/装载量:试验。3:in `<main> 

哪些是正确的行为,但并非我所希望的:

是否有办法将有关来源档案的yml文档装上,而不是与目前使用的DIR相比?

解决办法(载荷-试验):

require  yaml 
fn = File.dirname(File.expand_path(__FILE__)) +  /config.yml 
cnf = YAML::load(File.open(fn))
puts cnf[ Hello ]
最佳回答

你们应该通过:

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

EDIT:

自1月1日起,你可以简化并使用:

cnf = YAML::load_file(File.join(__dir__,  config.yml ))
问题回答

暂无回答




相关问题
Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

multiple ruby extension modules under one directory

Can sources for discrete ruby extension modules live in the same directory, controlled by the same extconf.rb script? Background: I ve a project with two extension modules, foo.so and bar.so which ...

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

热门标签