I have the following data and erb template: a hash mapping services to port numbers and an array of services.
I iterate over the array for each service and I need to access the hash to get the port number for the currently being processed service. I don't seem to be allowed to nest variables the way I am doing below. Is there a better way?
ports = {"max-api" => 83, "max-logger" => 82, "max-data" => 84}
services = %w( max_api max_data max_logger )
<% @services.each do |service| %>
<% if service.include?("max_logger") %>
shell: echo <%= service %>:<%= @ports["<%= service %>"] %>
<% else %>
shell: echo <%= service %>:<%= @ports["<%= service %>"] %>00
<% end %>
<% end %>
Simplest possible example I have the erb template
<% @services.each do |service| %>
<%= @ports[<%= service %>] %>
<% end %>
If I try to run my code I get the following error:
Chef::Mixin::Template::TemplateError
------------------------------------
(erubis):2: syntax error, unexpected '<', expecting ']'
_buf << ( @ports[<%= service ).to_s; _buf << ']...
^
(erubis):2: unterminated string meets end of file
<%- service %>or<%= service %>?,include?('max_logger')orservice == 'max_logger', is it@portsdifferent toports?max_loggervalue within the array, then every value you iterate won't print00, but if you check if suchservice == 'max_logger'then that will be just once, or more times if there are more than one'max_logger'in@services.