gpt4 book ai didi

ruby - 在带有 Ruby 的 YAML 中使用别名

转载 作者:数据小太阳 更新时间:2023-10-29 07:52:14 26 4
gpt4 key购买 nike

我有以下 YAML:

- PRO_PLAN:
- description: This is the Pro plan
publicName: Pro Plan
startDate: 12-20-2015

- PRO_MONTHLY_DIRECT:
- publicName: Pro Monthly
price: 20
sub_target: zone

- PRICING_COMPONENTS: &pro_entitlements
analytics_range: 21600
rules: 10
cannon: true


- PRO_ANNUAL_DIRECT:
- publicName: Pro Annual
price: 240
sub_target: zone

- PRICING_COMPONENTS:
<<: *pro_entitlements

结果数组没有为第二个节点引入 pro_entitlements:

[8] pry(BF)> app_hash[0]['PRO_PLAN'][1]
=> {"PRO_MONTHLY_DIRECT"=>[{"publicName"=>"Pro Monthly", "price"=>20, "sub_target"=>"zone"}, {"PRICING_COMPONENTS"=>nil, "analytics_range"=>21600, "page_rules"=>10, "polish"=>true}]}
[9] pry(BF)> app_hash[0]['PRO_PLAN'][2]
=> {"PRO_ANNUAL_DIRECT"=>[{"publicName"=>"Pro Annual", "price"=>240, "sub_target"=>"zone"}, {"PRICING_COMPONENTS"=>nil, "<<"=>nil}]}

最佳答案

当我需要构建更复杂的 YAML 文档时,我通常从使用 Ruby 和 Ruby 的 Hash 和 Array 对象开始。 YAML 序列化程序知道如何构建别名和 anchor ,如果我们允许,它会这样做:

require 'yaml'

foo = {'foo' => 1}
bar = {'bar' => 2, 'foo' => foo}
baz = {'baz' => 3, 'foo' => foo}

puts [foo, bar, baz].to_yaml

# >> ---
# >> - &1
# >> foo: 1
# >> - bar: 2
# >> foo: *1
# >> - baz: 3
# >> foo: *1

这里它为 foo 数组创建一个别名,然后在序列化哈希数组时引用它。

对 YAML 使用相同的想法:

require 'yaml'

PRO_ENTITLEMENTS = {
'analytics_range' => 21600,
'rules' => 10,
'cannon' => true
}

doc = [
{
'PRO_PLAN' =>
[
{
'description' => 'This is the Pro plan',
'publicName' => 'Pro Plan',
'startDate' => '12-20-2015'
},
{
'PRO_MONTHLY_DIRECT' =>
[
{
'publicName' => 'Pro Monthly',
'price' => 20,
'sub_target' => 'zone'
},
{
'PRICING_COMPONENTS' => PRO_ENTITLEMENTS,
'analytics_range' => 21600,
'rules' => 10,
'cannon' => true
}
]
},
{
'PRO_ANNUAL_DIRECT' =>
[
{
'publicName' => 'Pro Annual',
'price' => 240,
'sub_target' => 'zone'
},
{
'PRICING_COMPONENTS' => PRO_ENTITLEMENTS,
}
]
}
]
}
]

puts doc.to_yaml

运行它返回:

---
- PRO_PLAN:
- description: This is the Pro plan
publicName: Pro Plan
startDate: 12-20-2015
- PRO_MONTHLY_DIRECT:
- publicName: Pro Monthly
price: 20
sub_target: zone
- PRICING_COMPONENTS: &1
analytics_range: 21600
rules: 10
cannon: true
analytics_range: 21600
rules: 10
cannon: true
- PRO_ANNUAL_DIRECT:
- publicName: Pro Annual
price: 240
sub_target: zone
- PRICING_COMPONENTS: *1

这不能保证是您使用的正确输出,只是一个示例,说明如何在 Ruby 中构建结构并让 YAML 输出它,以便您可以看到它在序列化后应该是什么样子。

我们可以运行一个往返测试:

YAML.load(doc.to_yaml)
# => [{"PRO_PLAN"=>
# [{"description"=>"This is the Pro plan",
# "publicName"=>"Pro Plan",
# "startDate"=>"12-20-2015"},
# {"PRO_MONTHLY_DIRECT"=>
# [{"publicName"=>"Pro Monthly", "price"=>20, "sub_target"=>"zone"},
# {"PRICING_COMPONENTS"=>
# {"analytics_range"=>21600, "rules"=>10, "cannon"=>true},
# "analytics_range"=>21600,
# "rules"=>10,
# "cannon"=>true}]},
# {"PRO_ANNUAL_DIRECT"=>
# [{"publicName"=>"Pro Annual", "price"=>240, "sub_target"=>"zone"},
# {"PRICING_COMPONENTS"=>
# {"analytics_range"=>21600, "rules"=>10, "cannon"=>true}}]}]}]

关于ruby - 在带有 Ruby 的 YAML 中使用别名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32830082/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com