如何在YAML文件中使用变量?

我有YAML文件,需要将变量作为输入:

outputters: - type : DateFileOutputter name : logfile level : DEBUG date_pattern: '%Y%m%d' trunc : 'false' dirname : "/home/sameera/workspace/project/log" filename : "message.log" formatter : date_pattern: '%m/%d/%Y %H:%M:%S' pattern : '%d %l - %m' type : PatternFormatter 

我想将dirname作为参数传递,例如:

  dirname : "" 

我的LOGFILE_PATH在名为init.rb的文件中定义。

您可以使用ERB。

例如:

 template = ERB.new File.new("path/to/config.yml.erb").read processed = YAML.load template.result(binding) 

你可以在这里阅读有关绑定的更多信息: ruby元编程 。