如何编写既有效的ruby语法又有效的YAML语法的文件

时间:2014-04-10 08:33:22

标签: ruby yaml god polyglot

为了只对我的应用程序进行单点配置,我需要制作一个也是有效的ruby代码的YAML配置文件。即一个混合语法文件,可以解析为YAML并解析为ruby。

我的应用程序是由god gem管理的一套流程。我想为每个新配置文件加载一组新的维护进程(监视)。

上帝允许加载一个新的app.god(ruby)文件,并定义了新的手表,但我不想要一个app.god和app.yml,只需要一个文件。最简单的可能就是拥有app.god文件并在其中包含配置,但我更喜欢yml文件的想法,该文件也是有效的ruby代码。

1 个答案:

答案 0 :(得分:0)

#I found this that might be helpful:
#This is a valid ruby and a valid YAML file
#Comments are the same in YAML and ruby

true ?true: 
- <<YAML.to_i
# At this point in ruby it is the contents of a here doc (that will be 
# converted to an integer and negated if true happens not to be true)
# In YAML it is a hash with the first entry having key "true ?true"
# representing a list containing the string "- <<YAML.to_i"
# If your YAML object should be a list not a hash you could remove the first line

any_valid_yaml:  from here
a_list:
- or
- anything
- really
#Then mark the end of the YAML document with 
---
#And YAML is done and ignores anything from here on
#Next terminate the ruby here document
YAML

#Now we're in ruby world

@this = "pure ruby"

def anything(ruby)
   "here"
end