On Thursday, January 16, 2014 10:47:34 AM UTC-6, paul....@complex.com wrote: > > I have been trying to create a three level hash to dump into a template, > but the deepest layer of the hash seems to not work. > > My hash: > > $clusters = { > 'Default' => { > '127.0.0.1:11211' => { > 'hostname' => '127.0.0.1', > 'port' => '11211' > }, > }, > } > > My ERB template (modified for readability): > <% @clusters.sort.map do |k,v| -%> > <% if v.is_a?(Hash) -%> > '<%= k %>' > <% @clusters[k].sort.map do |ki, vi| -%> > <% if vi.is_a?(Hash) -%> > '<%= ki %>' > <% @clusters[ki].sort.map do |kii, vii| -%> > <% if vi and vi != '' -%> > '<%= kii %>' = '<%= vii %>', > <% end -%> > <% end -%> > <% end -%> > <% end -%> > <% end -%> > <% end -%> > > For some reason that I can't quite figure out, the innermost hash throws > a Detail: undefined method `sort' for nil:NilClass error. I assume if ki > was not a hash, the if vi.us_a?(Hash) would have been false and skipped > that sort. > > What am I doing wrong here? >
You're writing non-idiomatic Ruby, and you're embedding it into a template in a way that makes it hard to read. I would write that template more like this: <% @clusters.select { |k, v| v.is_a?(Hash) }.sort.each_pair do |k,v| -%> '<%= k %>' <% v.select { |ki, vi| vi.is_a?(Hash) }.sort.each_pair do |ki, vi| -%> '<%= ki %>' <% vi.select { |kii, vii| vii and vii != '' }.sort.each_pair do |kii, vii| -%> '<%= kii %>' = '<%= vii %>', <% end end end -%> Things to note: - You are iterating over key/value pairs, but you are not fully using the values provided to you. You test them for whether they are Hashes, but then you go back and retrieve them again via their keys. Relying strictly on the values passed into your blocks (as above) would avoid the indexing confusion you experienced and be clearer overall. - Embedding each Ruby line into your template separately makes everything harder to follow. Moreover, doing it in the way you have done puts a bunch of extra whitespace into the template output. (My version differed more from the original in this regard before I reduced the number of separate lines of Ruby code.) - The each_pair() method is better for your iteration purposes than is map() because each_pair() makes your intent clearer. It's pointless and somewhat confusing to use map() and then throw away the result. - Using select() to filter your hashes prior to iteration, as above, is more idiomatic and clearer than testing the iterated values. In this case it also cuts the number of levels of nesting in half. - In the innermost 'if' statement of your original code, you are testing variable 'vi', whereas it looks like you should be to testing 'vii' instead. John -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/97c66547-d705-4370-acff-e5202c1a26ec%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.