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
精彩评论