开发者

Look for a configuration file with Ruby

I written a Ruby tool, named foobar, having a default configuration into a file (called .foobar).

When the tool is executed without any params, the configuration file's params can be used: ~/.foobar.

But, if the current tool's path is ~/projects/foobar and if ~/projects/foobar/.foobar exists, then this file should be used instead of ~/.foobar.

That's开发者_开发技巧 why the way to look for this configuration file should start from the current folder until the current user folder.

Is there a simple way to look for this file?


cfg_file = File.open(".foobar", "r") rescue File.open("~/.foobar", "r")

Although to be honest, I almost always do two things: provide an option for a config file path, and allow overriding of default config values. The problem with the latter is that unless you know config files are ordered/have precedence, it can be confusing.


I would do this:

if File.exists(".foobar")
    # open .foobar
else
    # open ~/..
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜