0

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
6
  • Sorry to ask in such annoying way but, is it <%- service %> or <%= service %>?, include?('max_logger') or service == 'max_logger', is it @ports different to ports? Commented May 22, 2017 at 4:06
  • <%= service %> is the correct way, - was a mistake. The include statement for checking whether or not max_logger is being processed is correct. My understanding with @ports is that I need the @ for accessing externally defined variables that are passed to the erb. Therefore do not require it for service, which is defined within the erb template. Commented May 22, 2017 at 4:10
  • I see, help me to understand, if you check for the presence of a max_logger value within the array, then every value you iterate won't print 00, but if you check if such service == 'max_logger' then that will be just once, or more times if there are more than one 'max_logger' in @services. Commented May 22, 2017 at 4:12
  • What I am checking there is if the current iteration of services is max_logger. max_logger gets a special command ran that nothing else gets. max_logger will NOT print 00, but api and data WILL print 00. Commented May 22, 2017 at 4:16
  • My naming conventions are poor. Each service - api, logger, data - may actually run several daemons. Each daemon runs operates on a specific range of ports. What I really need to do is for each service is generate a config that includes all daemons. max_logger has the daemons max-logger and max-analytics, analytics being on port 1 and logger being on port 2. I may have oversimplified the examples in the original question if that isn't clear Commented May 22, 2017 at 4:19

1 Answer 1

2

I realized lately they won't work if they don't match in name (array value, hash key):

{"max-api" => 83, "max-logger" => 82, "max-data" => 84}

They differ in - and/or _ from @services:

%w( max_api max_data max_logger )

Just tweaked a bit @services:

ports = {'max-api' => 83, 'max-logger' => 82, 'max-data' => 84}
services = %w(max-api max-data max-logger)

services.each do |service|
  puts "shell: echo #{service}:#{ports[service]}#{'00' if service != 'max-logger'}"
end
# => shell: echo max-api:8300
# => shell: echo max-data:8400
# => shell: echo max-logger:82
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.