我有以下木偶示例模板:
{
"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
} ],
}
将其与 market=['UK','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
} ],
}
问题是最后一个逗号,这使它成为无效的 solr 配置。
我可以使用market.map 和join(','),而不是应用markets.each。但是在这种情况下如何使用地图?
我可以按如下方式使用地图:
<%= @markets.map{ |market| "hello_"+market }.join(',') -%>
这将打印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
} ],
}
这不起作用。
那么,如何让它工作呢?或者如何修改我的人偶模板以删除最后一个逗号?