如何展平YAML文件,使其不具有别名(红宝石)?

时间:2019-04-09 15:59:46

标签: ruby yaml

假设我有:

-: &defaults
  client_id: 'invalid_client_id'
  client_secret: 'secret'

production:
  <<: *defaults
staging:
  <<: *defaults

,我想阅读然后将其展平,因此没有别名。我该怎么做?我会更喜欢一个利用当前YAML库的某些配置而不是扔在一起的解决方案的解决方案(如果可行)。

IOW:

production:
  client_id: 'invalid_client_id'
  client_secret: 'secret'
staging:
  client_id: 'invalid_client_id'
  client_secret: 'secret'

编辑:

从YAML.load执行YAML.dump的建议有效,但它在文档中保留了原始别名:

---
"-":
  client_id: invalid_client_id
  client_secret: secret
production:
  client_id: invalid_client_id
  client_secret: secret
staging:
  client_id: invalid_client_id
  client_secret: secret

这不是我真正需要的。很高兴接受答案,但想看看是否还有其他想法。

2 个答案:

答案 0 :(得分:1)

只需使用[CanBeNull] public delegate string ReturnMaybeNull(); [NotNull] public delegate string ReturnNotNull([NotNull]string someParam); [NotNull] private readonly ReturnMaybeNull FunctionThatMayReturnNull = () => null; [NotNull] private readonly ReturnNotNull FunctionThatNeverReturnsNull = someParam => null; // no warning void Test() { bool test = FunctionThatMayReturnNull().Equals(""); // no warning string thisStringIsNotNull = FunctionThatNeverReturnsNull(null); // parameter warning here if (thisStringIsNotNull == null) // no warning { test = test ^ true; } }

YAML.dump

答案 1 :(得分:1)

在修改后的问题中您想要的内容打破了YAML本身。在这种情况下,您应该手动删除初始别名:

require 'yaml'
yml = YAML.load(File.open 't.yml')
yml.delete('-')
yml.to_yaml
相关问题