一个 YAML 对象可以引用另一个吗?
我想让一个 yaml 对象引用另一个,如下所示:
intro: "Hello, dear user."
registration: $intro Thanks for registering!
new_message: $intro You have a new message!
上面的语法只是它如何工作的一个示例(这也是它在 此 cpan 模块。)
我正在使用标准 ruby yaml 解析器。
这可能吗?
I'd like to have one yaml object refer to another, like so:
intro: "Hello, dear user."
registration: $intro Thanks for registering!
new_message: $intro You have a new message!
The above syntax is just an example of how it might work (it's also how it seems to work in this cpan module.)
I'm using the standard ruby yaml parser.
Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一些 yaml 对象确实引用其他对象:
请注意,它并不总是重用对象(字符串只是重复),但有时会重用(哈希值定义一次并通过引用重用)。
YAML 不支持字符串插值 - 这似乎是您想要做的 - 但没有理由您不能对其进行更详细的编码:
然后您可以在加载 YAML 后对其进行插值:
这将产生以下为
插值
:Some yaml objects do refer to the others:
Note that it doesn't always reuse objects (the string is just repeated) but it does sometimes (the hash is defined once and reused by reference).
YAML doesn't support string interpolation - which is what you seem to be trying to do - but there's no reason you couldn't encode it a bit more verbosely:
Then you can interpolate it after you load the YAML:
And this will yield the following for
interpolated
:与其尝试在 yaml 中使用隐式引用,为什么不使用替换字符串(如上面所示,但需要引号)并在解析时显式替换它们的内容?
Rather than trying to use implicit references in your yaml, why don't you use substitution strings (like you show above, you need quotes though) and explicitly substitute the contents of them at parse time?