开发者

YAML Config - best way to replace symbols within a string

I have a configuration file in YAML format with some开发者_如何学Go paths in my rails applications like this:

config
    :path1: /a/b/c
    :path2: /a/:id/c

path1 doesn't helps too much. I need that every time I use config[:path2] to somehow replace the :id with a value I have. It is possible?

Hope this was clear enough. Thanks!


You can use a bit of ERB.

# config.yml
config
    :path1: /a/b/c
    :path2: /a/<%= id %>/c
    :path3: <%= path3 %>

and

id = "23"
path3 = "hello/world"

t = ERB.new(File.read("config.yml"))
c = YAML.load(t.result(binding).to_s)

c["config"][:path2]
# => /a/23/c
c["config"][:path3]
# => /hello/world
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜