I have the following puppet example template:
我有以下puppet示例模板:
{
"servers" : [ {
"port" : 9200,
"host" : "localhost",
"queries" : [
<% @markets.each do |market| -%>
{
"outputWriters" : [ { "@class" : "com.googlecode.jmxtrans.model.output.StdOutWriter" } ],
"obj" : "solr/market_<%= market %>:type=queryResultCache,id=org.apache.solr.search.LRUCache",
"attr" : [ "hits","hitratio" ]
},
<% end -%>
],
"numQueryThreads" : 2
} ],
}
Applying it with market=['UK','FR','IT'], I get the following:
将它应用于市场= ['英国','FR','IT'],我得到以下结果:
{
"servers" : [ {
"port" : 9200,
"host" : "localhost",
"queries" : [
{
"outputWriters" : [ { "@class" : "com.googlecode.jmxtrans.model.output.StdOutWriter" } ],
"obj" : "solr/market_UK:type=queryResultCache,id=org.apache.solr.search.LRUCache",
"attr" : [ "hits","hitratio" ]
},
{
"outputWriters" : [ { "@class" : "com.googlecode.jmxtrans.model.output.StdOutWriter" } ],
"obj" : "solr/market_FR:type=queryResultCache,id=org.apache.solr.search.LRUCache",
"attr" : [ "hits","hitratio" ]
},
{
"outputWriters" : [ { "@class" : "com.googlecode.jmxtrans.model.output.StdOutWriter" } ],
"obj" : "solr/market_IT:type=queryResultCache,id=org.apache.solr.search.LRUCache",
"attr" : [ "hits","hitratio" ]
},
],
"numQueryThreads" : 2
} ],
}
The problem is the last comma, which makes it an invalid solr config.
问题是最后一个逗号,这使得它成为无效的solr配置。
Instead of applying markets.each do, I could use market.map and join(','). but how to use map in this case?
而不是应用市场。我可以使用market.map和join(',')。但在这种情况下如何使用地图?
I can use map as follows:
我可以使用如下地图:
<%= @markets.map{ |market| "hello_"+market }.join(',') -%>
this would print hello_UK,hello_FR,hello_IT
(note that we don't have a comma after hello_IT), but I would need something like this:
这会打印hello_UK,hello_FR,hello_IT(注意我们在hello_IT之后没有逗号),但我需要这样的东西:
{
"servers" : [ {
"port" : 9200,
"host" : "localhost",
"queries" : [
<% @markets.map |market| -%>
{
"outputWriters" : [ { "@class" : "com.googlecode.jmxtrans.model.output.StdOutWriter" } ],
"obj" : "solr/market_<%= market %>:type=queryResultCache,id=org.apache.solr.search.LRUCache",
"attr" : [ "hits","hitratio" ]
},
<% }.join(',') -%>
],
"numQueryThreads" : 2
} ],
}
this does not work.
这不起作用。
so, how to make it work? or how to modify my puppet template to remove the last comma?
那么,如何让它发挥作用?或者如何修改我的木偶模板以删除最后一个逗号?
2 个解决方案
#1
17
This is actually a Ruby problem. Since this is an array, just change the .each to .each_with_index. Then you can wrap the final comma in a check to see whether the current index value is one less than the size of the index. So
这实际上是一个Ruby问题。由于这是一个数组,只需将.each更改为.each_with_index即可。然后,您可以将最终逗号包装在一个检查中,以查看当前索引值是否小于索引的大小。所以
<% @markets.each_with_index |market, i| -%>
and then
}<%= ',' if i < (@markets.size - 1) %>
#2
3
I saw on a puppet labs site (https://ask.puppetlabs.com/question/4195/joining-array-from-hieraconcat-other-value-in-erb/) this form of solution.
我在一个木偶实验室网站(https://ask.puppetlabs.com/question/4195/joining-array-from-hieraconcat-other-value-in-erb/)上看到了这种形式的解决方案。
<%= quorum.map{ |srv| "#{srv}:#{portNum}" }.join(',') %>
This code lets you take an array and create a variation of each element then join them all together with the appropriate join text (not including a spurious comma at end).
此代码允许您获取数组并创建每个元素的变体,然后将它们与适当的连接文本(最后不包括虚假逗号)连接在一起。
#1
17
This is actually a Ruby problem. Since this is an array, just change the .each to .each_with_index. Then you can wrap the final comma in a check to see whether the current index value is one less than the size of the index. So
这实际上是一个Ruby问题。由于这是一个数组,只需将.each更改为.each_with_index即可。然后,您可以将最终逗号包装在一个检查中,以查看当前索引值是否小于索引的大小。所以
<% @markets.each_with_index |market, i| -%>
and then
}<%= ',' if i < (@markets.size - 1) %>
#2
3
I saw on a puppet labs site (https://ask.puppetlabs.com/question/4195/joining-array-from-hieraconcat-other-value-in-erb/) this form of solution.
我在一个木偶实验室网站(https://ask.puppetlabs.com/question/4195/joining-array-from-hieraconcat-other-value-in-erb/)上看到了这种形式的解决方案。
<%= quorum.map{ |srv| "#{srv}:#{portNum}" }.join(',') %>
This code lets you take an array and create a variation of each element then join them all together with the appropriate join text (not including a spurious comma at end).
此代码允许您获取数组并创建每个元素的变体,然后将它们与适当的连接文本(最后不包括虚假逗号)连接在一起。