1

I have the following my_script.ruby

require 'erb'
require 'ostruct'
namespace = OpenStruct.new(:jmx_port => 9200, :markets=> ['CH', 'FR'])
template = File.open("my.json.erb", "rb").read;
puts ERB.new(template).result(namespace.instance_eval { binding })

and my.json.erb:

{
  "servers" : [ {
    "port" : "<%= jmx_port %>",
    "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
  } ]
}

but executing the script by irb my_script.ruby I get this error:

?> puts ERB.new(template).result(namespace.instance_eval { binding })
SyntaxError: compile error
(erb):10: syntax error, unexpected ';'
;  @markets.each do |market| -; _erbout.concat "\n      "

                           ^

for context, my.json.erb file is a puppet file, and using my_script.ruby I am trying to verify that the file is correct, before sending it to puppet.

what am I doing wrong?

ps: here it is the erb template, used by puppet: http://docs.puppetlabs.com/guides/templating.html

0

1 Answer 1

2

There are a couple of issues with your erb:

  • You should use <% %> instead of <% -%> for things you don't want to output.
  • Since you're using an OpenStruct, you should use markets instead of @markets

Here's the final version of the my.json.erb file working:

{
  "servers" : [ {
    "port" : "<%= jmx_port %>",
    "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
  } ]
}
Sign up to request clarification or add additional context in comments.

5 Comments

David is correct. Why we can't use <% -%> i saws that ruby doc ruby-doc.org/stdlib-2.0/libdoc/erb/rdoc/ERB.html the syntax is available
@KitHo that's new to ruby 2.0. What version are you using?
ruby 1.9.3 seems u are correct ruby-doc.org/stdlib-1.9.3/libdoc/erb/rdoc/ERB.html it doesn't provider <% -%> possibly, the puppet has provide <% -%> on its default, and that why the OP has problem if he using 1.9.3 in ruby purely
thanks, tomorrow I'll upgrade ruby 2.0 and test it again; about writing markets instead of @markets, can you please elaborate? my goal is to verify a puppet configuration, and this requires to use @markets.
@DavidPortabella An OpenStruct is like a wrapper around a ruby hash that gives you friendly accessor methods, which in your case are markets and jmx_port. There are no instance variables in an OpenStruct. In your case, it means that you can only access the markets array by using the markets method. There's no instance variable called @markets.

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.